日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

20200714一维数组的经典例题(成绩的最高分,最低分;猜中数字游戏;数组的增添改查;)

發布時間:2024/8/1 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 20200714一维数组的经典例题(成绩的最高分,最低分;猜中数字游戏;数组的增添改查;) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.計算5名同學成績的總分,平均分,最高分,最低分
實現步驟:

  • 定義一個長度為5的整型數組;
  • 定義兩個float類型變量,用于保存總成績、平均分,初始值為0;
  • 定義兩個int類型變量,用于保存最高分和最低分,初始值為0;
  • 從控制臺接收5名同學的成績;
  • 通過循環使數組的5個元素相加得到總成績;
  • 通過循環遍歷數組并比較元素大小,得到最高分和最低分。
  • public class Zuoye1 {public static void main(String[] args) {int scores[]=new int[5];//定義一個長度為5的整型數組float sum=0;float avg=0;int max=0;int min=0;Scanner sc=new Scanner(System.in);System.out.println("請輸入五位學生的筆試成績:");for (int i = 0; i <scores.length ; i++) {scores[i]=sc.nextInt();}max=scores[0];min=scores[0];for (int i = 0; i <scores.length ; i++) {sum+=scores[i];if(scores[i]>max){max=scores[i];}if(scores[i]<min){min=scores[i];}}avg=sum/scores.length;System.out.println("總成績為:"+sum);System.out.println("平均成績是:"+avg);System.out.println("最高分為:"+max);System.out.println("最低分為:"+min);} }


    二.有一個數列:8,4,2,1,23,344,12

    • 循環輸出數列的值
    • 求數列中所有數值的和
    • 猜數游戲:從鍵盤中任意輸入一個數據,判斷數列中是否包含此數
    public class Zuoye2 {public static void main(String[] args) {int nums[]={8,4,2,1,23,344,12};int sum=0;Scanner sc=new Scanner(System.in);for (int i = 0; i < nums.length; i++) {System.out.print(nums[i]+" ");sum+=nums[i];}System.out.println();System.out.println("整個數列的和為:"+sum);System.out.println("輸入任意一個數據:");int m=sc.nextInt();int l=-1;for (int i = 0; i < nums.length; i++) {if(m==nums[i]){l=m;break;}}if(l!=-1){System.out.println(l+"在數列中,猜中數字");}else{System.out.println(m+"不在這個數列中,未猜中數字");}}}


    三.數組添加,修改,刪除,插入
    1.數組添加:在數列里增加iPhone5s

    public class Zuoye4 {public static void main(String[] args) {int index=-1;String[]phones={"iPhone4","iPhone4S","iPhone5",null};for(int i=0;i<phones.length;i++){if(phones[i]==null){index=i;break;}}if(index!=-1){phones[index]="iPhone5S";for (int i = 0; i <phones.length ; i++) {System.out.println(phones[i]);}}else{System.out.println("數組已滿");}} }


    2.數組修改:將iPhone5改成iPhone5掉漆

    public class Zuoye3 {public static void main(String[] args) {int index=-1;String[]phones={"iPhone3GS經典","iPhone4s革新","iPone4S變化不大","iPhone5"};for (int i = 0; i <phones.length ; i++) {if (phones[i].equals("iPhone5")) {index=i;break;}}if(index!=-1){phones[index]="iPhones掉漆";for (int i = 0; i <phones.length; i++) {System.out.println(phones[i]);}}else{System.out.println("沒有iPhone5");}} }


    3.數組刪除:刪掉iPhone3GS經典

    public class Zuoye5{public static void main(String[] args) {String[]phones={"iPhone3GS經典","iPhone4革新","iPhone4S變化不大","iPhone5掉漆"};int index=-1;for (int i = 0; i <phones.length ; i++) {if(phones[i].equals("iPhone3GS經典")){index=i;break;}}if(index!=-1){for (int i = index; i <phones.length-1 ; i++) {phones[i]=phones[i+1];}phones[phones.length-1]=null;}else{System.out.println("沒有你要刪除的內容");}for (int i = 0; i <phones.length ; i++) {System.out.println(phones[i]);}} }


    4.數組插值:向數組中插入一個元素.

    • 有一組學員的成績{99,85,82,63, 60},將它們按降序排列。
    • 要增加一個學員的成績,將它插入成績序列,并保持降序
    public class Zuoye6 {public static void main(String[] args) {int[]scores={99,85,82,63,60,0};int index=-1;Scanner sc=new Scanner(System.in);System.out.println("請輸入新學員的成績:");int score1=sc.nextInt();for (int i = 0; i < scores.length; i++) {if(score1<scores[i]&&score1>scores[i+1]){index=i+1;break;}}if(index!=-1){for (int i=scores.length-2;i>=index;i--){scores[i+1]=scores[i];}scores[index]=score1;}for (int i = 0; i <scores.length ; i++) {System.out.print(scores[i]+" ");}} }


    三.求出4家店的最低手機價格
    步驟:

    • 1.定義數組存儲價格,并利用循環輸入
    • 2.定義變量min保存當前的最低價
    • 3.將min和數組中的其余元素依次比較
    public class Zuoye9 {public static void main(String[] args) {Scanner sc=new Scanner(System.in);int price[]=new int[4];System.out.println("請輸入四家店的價格");for (int i = 0; i <price.length; i++) {System.out.println("請輸入第"+(i+1)+"門店的價格為:");price[i]=sc.nextInt();}int min=price[0];for (int i = 0; i < price.length; i++) {if(price[i]<min){min=price[i];}}System.out.println("最低價格是:"+min);} }


    四.數組存儲5筆購物金額,在控制臺輸出并計算總金額

    public class Zuoye10 {public static void main(String[] args) {System.out.println("請輸入會員本月的消費記錄");double[]consume=new double[5];Scanner sc=new Scanner(System.in);double sum=0;for (int i = 0; i <consume.length; i++) {System.out.print("請輸入第"+(i+1)+"筆消費金額:");consume[i]=sc.nextDouble();//System.out.println();}System.out.println("序號"+" 金額(元)");for (int i = 0; i < consume.length; i++) {System.out.println((i+1)+" "+consume[i]);sum+=consume[i];}System.out.println("總金額 "+sum);} }

    總結

    以上是生活随笔為你收集整理的20200714一维数组的经典例题(成绩的最高分,最低分;猜中数字游戏;数组的增添改查;)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 国产精品久久久久久人妻精品动漫 | 欧美xxxx888| 一区二区三区四区欧美 | 91爱爱·com| 久久二区三区 | 日韩免费片 | 欧美精品123区 | 美女扒开腿让人桶爽原神 | 久草资源福利 | 日韩视频免费观看 | 日韩成人av网| 国产小视频免费在线观看 | 少妇激情偷人三级 | 不卡的毛片 | 中文字幕黄色av | 僵尸叔叔在线观看国语高清免费观看 | 欧美一区二区三区免费在线观看 | 久久人人澡 | 91黄址 | 丁香花国语版普通话 | 性xxxxxxxxx| 中文字幕有码在线播放 | 青青青免费在线视频 | 中文字幕在线观看日韩 | 成人a网| 亚洲国产精品久久久久婷蜜芽 | 91免费在线看 | 五十路在线 | 日韩精品在线观看视频 | 亚洲国产欧美一区 | 午夜毛片视频 | 在线免费视频 | 波多野结衣片子 | 草女人视频 | 欧美1区2区 | 激情视频在线免费观看 | 欧美放荡办公室videos4k | 亚洲精品乱码久久久久久自慰 | 亚洲色图88 | 久久精品天天中文字幕人妻 | 国产精品黑人一区二区三区 | 欧亚乱熟女一区二区在线 | 美女扒开腿男人爽桶 | 永久免费未满蜜桃 | 狠狠av | 国产特黄级aaaaa片免 | 天天躁日日躁狠狠躁av | 日本中出视频 | 日b视频免费观看 | 黄色在线视频网站 | 久久91av| 国产精品久久777777毛茸茸 | 中文字幕无码人妻少妇免费 | 老女人黄色片 | 午夜精品一区二区三区在线播放 | 一级做a爱片性色毛片 | 新亚洲天堂 | 日本www在线观看 | 亚欧洲精品| 国产乱轮视频 | 国产精品国产一区二区三区四区 | 北条麻妃一区二区三区四区五区 | 潮见百合子 | 桃色91| 亚洲jlzzjizz少妇 | 潘金莲激情呻吟欲求不满视频 | 爱情岛亚洲首页论坛小巨 | 人妖性做爰aaaa | 强制高潮抽搐哭叫求饶h | 亚洲爆乳无码精品aaa片蜜桃 | xxxxxx国产 | 诱人的乳峰奶水hd | 看片一区 | 伊人自拍 | 久草视频在线播放 | 国产色中色 | av播放在线| 豆豆色成人网 | 日本免费在线视频 | 伊人网在线免费观看 | 国产男女猛烈无遮挡免费视频 | 久久综合久 | 国产专区一区二区 | 国产伦精品一区二区 | 极品尤物一区二区三区 | 精品一区二区无码 | 黄色av成人| 久久三级 | 日本高清不卡视频 | 操极品少妇 | 99视频网址 | 免费黄色激情视频 | 免费视频久久 | 午夜毛片| 国产无遮挡又黄又爽又色视频 | 国产a线 | 午夜伦伦电影理论片费看 | 亚洲区一区二区 | xvideos成人免费视频 |