例1:求學生的平均分 1 public static void main(String[] args) { 2 Scanner input=new Scanner(System.in); 4 int scores []=new int[5]; 5 int sum=0; 6 7 System.out.
例1:求學生的平均分
1 public static void main(String[] args) { 2 Scanner input=new Scanner(System.in); 4 int scores []=new int[5]; 5 int sum=0; 6 7 System.out.println("請輸入五位同學的成績:"); 8 for (int i = 0; i < scores.length; i++) {//遍曆數組 9 scores[i]=input.nextInt(); 10 sum+=scores[i];//成績累加 11 } 12 System.out.print("平均分:"+sum/scores.length);
例2:輸出5筆購物金額及總金額
1 public static void main(String[] args) { 2 Scanner input=new Scanner(System.in); 3 double sum=0; 4 double scores[]=new double[5]; 5 for (int i = 0; i < scores.length; i++) { 6 System.out.print("請輸入第"+(i+1)+"筆金額的記錄:"); 7 scores[i]=input.nextDouble(); 8 sum+=scores[i]; 9 } 10 System.out.println("序號\t\t"+"金額(元)"); 11 for (int i = 0; i < scores.length; i++) { 12 System.out.print((i+1)+"\t\t"); 13 System.out.println(scores[i]); 14 } 15 System.out.println("總金額:\t\t"+sum);
例3:迴圈錄入5位學員成績,進行升序排列後輸出結果
1 public static void main(String[] args) { 2 Scanner input=new Scanner(System.in); 3 int scores []=new int[5]; 4 System.out.println("請輸入5位同學的成績:"); 5 //錄入成績 6 for (int i = 0; i < scores.length; i++) { 7 scores[i]=input.nextInt(); 8 } 9 Arrays.sort(scores);//排序 10 System.out.println("學員成績按升序排序:"); 11 for (int i = 0; i < scores.length; i++) { 12 System.out.println(scores [i]+" "); 13 }
例4:從鍵盤輸入本次Java考試五位學生的成績,求考試成績最高分
1 public static void main(String[] args) { 2 Scanner input=new Scanner(System.in); 3 int scores []=new int[5]; 4 int max=0; 5 System.out.println("請輸入5位同學的成績:"); 6 //錄入成績 7 for (int i = 0; i < scores.length; i++) { 8 scores[i]=input.nextInt(); 9 } 10 11 for (int i = 0; i < scores.length; i++) { 12 if (scores[i]>max) { 13 max=scores[i]; 14 } 15 } 16 System.out.println("最高分:"+max); 17 }
例5:有一組學員的成績{99,85,82,63, 60},將它們按升序排列。要增加一個學員的成績,將它插入成績序列,並保持升序。
1 public static void main(String[] args) { 2 Scanner input=new Scanner(System.in); 3 int [] list=new int [6]; 4 list[0]=99; 5 list[1]=95; 6 list[2]=92; 7 list[3]=89; 8 list[4]=69; 9 list[5]=49; 10 int index=list.length;//保存新增成績的位置 11 System.out.println("請輸入新增成績:"); 12 int num=input.nextInt();//輸入要插入的數據 13 //找到新元素插入的位置 14 for (int i = 0; i < list.length; i++) { 15 16 if (num>list[i]) { 17 index=i; 18 break; 19 } 20 } 21 //元素後移 22 for (int i =list.length-1 ; i >index ; i--) { 23 list[i]=list[i-1];//index下標開始的元素後移一個位置 24 } 25 list[index]=num; 26 System.out.println("插入成績的下標:"+index); 27 28 System.out.println("插入後成績信息是:"); 29 for (int i = 0; i < list.length; i++) { 30 System.out.println(list[i]+"\t"); 31 } 32 }
例6:將 一組亂序的字元進行排序 進行升序和逆序輸出
1 public static void main(String[] args) { 2 Scanner input=new Scanner(System.in); 3 String[]num=new String[]{"a","c","u","b","e","p","f","z"}; 4 System.out.print("原字元序列:"); 5 for (int i = 0; i < num.length; i++) { 6 System.out.print(num[i]+" "); 7 } 8 Arrays.sort(num); 9 System.out.println();//換行 10 System.out.print("升序排序後:"); 11 for (int i = 0; i < num.length; i++) { 12 System.out.print(num[i]+" "); 13 } 14 System.out.println();//換行 15 System.out.print("逆序輸出為:"); 16 //逆序,則從最後的哪一個元素排在第一位 17 for (int i = num.length-1; i >=0 ; i--) { 18 System.out.print(num[i]+" "); 19 }
例7:求出4家店的最低手機價格及原始位置(下標)
1 Scanner input=new Scanner(System.in); 2 System.out.println("請輸入4家店的價格"); 3 int[]num=new int[4]; 4 for (int i = 0; i < num.length; i++) { 5 System.out.print("第"+(i+1)+"店的價格:"); 6 num[i]=input.nextInt(); 7 } 8 int min=num[0]; 9 int index=0; 10 for (int j = 0; j < num.length; j++) { 11 if (num[j]<min) { 12 min=num[j]; 13 index=j; 14 } 15 16 } 17 System.out.print("最低價格:"+min); 18 System.out.println("且它在數組中的原始位置(下標)是:" + index); 19 } 20 }
例8:從鍵盤上輸出10個整數,合法值為1,2或3,其餘為不合法,並且統計合法及不合法的個數
1 Scanner input=new Scanner(System.in); 2 int nums[] = new int[10]; 3 int a = 0; 4 int b = 0; 5 int c = 0; 6 int d = 0; 7 System.out.println("請輸入10個數:"); 8 for (int i = 0; i < nums.length; i++) { 9 nums[i] = input.nextInt(); 10 11 switch (nums[i]) { 12 case 1: 13 a++; 14 break; 15 case 2: 16 b++; 17 break; 18 case 3: 19 c++; 20 break; 21 default: 22 d++; 23 break; 24 } 25 26 } 27 System.out.println("數字1的個數:"+a); 28 System.out.println("數字2的個數:"+b); 29 System.out.println("數字3的個數:"+c); 30 System.out.println("非法數字的個數:"+d);
例9:假設有一個數組,長度為5,int [] aray=new int[]{1,3,-1,5,-2},先創建一個新數組,要求新數組的存放順序與原數組的元素逆序,並且如果原數組中的元素值小於0,在新數組中安0存儲,
1 Scanner input=new Scanner(System.in); 2 3 int []array=new int[]{1,3,-1,5,-2}; 4 System.out.println("原數組的為:"); 5 for (int i = 0; i < array.length; i++) { 6 System.out.print(array[i]+ " "); 7 } 8 System.out.println(); 9 int newarray[]=new int[5]; 10 for (int i = array.length - 1; i >= 0; i--) { 11 if (array[i] < 0) { 12 continue; 13 } 14 if (array[i] > 0) { 15 newarray[array.length - i - 1] = array[i]; 16 } 17 } 18 System.out.println(""); 19 System.out.println("逆序並處理後的數組為:"); 20 for (int i = 0; i < newarray.length; i++) { 21 System.out.print(newarray[i]+" "); 22 } 23 }
例10:
1 public static void main(String[] args) { 2 Scanner input=new Scanner(System.in); 3 String[] musics = new String[]{"Island","Ocean","Pretty","Sun"}; 4 int index = musics.length; //保存新歌插入位置 5 //輸出插入前的結果 6 System.out.print("插入前的數組為:"); 7 for(int i = 0; i < musics.length ; i++){ 8 System.out.print(musics[i]+" "); 9 } 10 //新數組 11 String[] newMusics = new String[musics.length+1];//新歌曲數組 12 String music = ""; //保存用戶輸入的歌曲名稱 13 //將數組musics中的元素複製到新歌曲數組newMusics中 14 for(int i = 0; i < musics.length; i++){ 15 newMusics[i] = musics[i]; 16 } 17 //輸入歌曲名稱 18 System.out.print("\n請輸入歌曲名稱:"); 19 music = input.nextLine(); 20 //找到新元素的插入位置 21 for(int i = 0; i < musics.length; i++){ 22 if(musics[i].compareToIgnoreCase(music) > 0){ 23 index = i; 24 break; 25 } 26 } 27 //元素後移 28 for(int i = newMusics.length-1; i > index; i--){ 29 newMusics[i] = newMusics[i-1]; //index下標開始的元素後移一個位置 30 } 31 newMusics[index] = music; //新元素放在index的位置 地址 32 System.out.print("逆序處理後的數組為:"); 33 for (int i = 0; i < newMusics.length; i++) { 34 System.out.print(newMusics[index]+" " ); 35 } 36 37 } 38 39 }