天天看點

java代碼示例(5)

1 /**
 2  * 需求分析:從鍵盤輸入5名學員某門課程的筆試成績,并求出五門成績的總成績和平均成績
 3  * @author chenyanlong
 4  * 日期:2017/10/14
 5  */
 6 package com.hp.test05;
 7 
 8 import java.util.Scanner;
 9 
10 public class HS_Array1 {
11 
12     public static void main(String[] args) {
13         // TODO Auto-generated method stub
14         System.out.println("請輸入5個學生的筆試成績:");
15         
16         double sum=0;
17         double avg;
18         int[] scores = new int[6];
19         Scanner input=new Scanner(System.in);
20         //輸入5個成績
21         for(int i=0;i<5;i++){
22             
23             scores[i]=input.nextInt();
24         }
25         //求和
26         for(int j=0;j<5;j++){
27             sum=sum+scores[j];
28         }
29         System.out.println("總成績:"+sum);
30         //平均分
31         avg=sum/5;
32         System.out.println("平均分:"+avg);           
33     }
34 }      
1 /**
 2  * 需求分析: 從鍵盤輸入5名學員某門課程的筆試成績,
 3  * 并求出五門成績的最高分、最低分和平均分
 4  * @author chenyanlong
 5  * 日期:2017/10/14
 6  */
 7 package com.hp.test05;
 8 
 9 import java.util.Scanner;
10 
11 public class HS_Array2 {
12 
13     public static void main(String[] args) {
14         
15         int[] scores=new int[5];
16         int sum=0;
17       
18         double avg;
19         System.out.println("請輸入5個成績");
20         
21         Scanner input=new Scanner(System.in);
22         for(int i=0;i<scores.length;i++){           
23             scores[i]=input.nextInt();
24         }
25         //總成績,最高分,最低分
26         int max=scores[0];
27         int min=scores[0];
28         for(int j=0;j<scores.length;j++){
29             //總成績
30             sum +=scores[j];
31             //最低分
32             if(scores[j]<min){
33                 min=scores[j];
34             }
35             //最低分
36             if(scores[j]>max){
37                 max=scores[j];
38             }
39         }
40         avg=sum/scores.length;
41         System.out.println("總成績:"+sum);
42         System.out.println("最高分:"+max);
43         System.out.println("最低分:"+min);
44         System.out.println("平均分:"+avg);
45             
46     }
47 }      
1 /**
 2  * 需求分析: 用二維數組存放三個班學生的成績,并計算三個班學生的總成績    
 3  * @author chenyanlong
 4  * 日期:2017/10/14
 5  */
 6 package com.hp.test05;
 7 
 8 import java.util.Arrays;
 9 import java.util.Scanner;
10 
11 public class HS_Array3 {
12 
13     public static void main(String[] args) {
14         
15         int[][] array=new int[][]{{2,1},{2,1,3},{2,1}};
16         for(int i=0;i<array.length;i++){
17             String str=(i+1)+"班";
18             Arrays.sort(array[i]);
19             System.out.println(str+"排序以後");
20             for(int j=0;j<array[i].length;j++){
21                 System.out.println(array[i][j]);
22             }
23         }
24     }
25 }      
1 /**
 2  * 需求分析: 用二維數組存放三個班學生的成績,并對三個班級學生成績進行排序
 3  * @author chenyanlong
 4  * 日期:2017/10/14
 5  */
 6 package com.hp.test05;
 7 
 8 import java.util.Scanner;
 9 
10 public class HS_Array4 {
11 
12     public static void main(String[] args) {
13         
14         int[][] array=new int[][]{{1,2},{1,2,3},{1,2}};
15         
16         for(int i=0;i<array.length;i++){
17             String str=(i+1)+"班";
18             int total=0;
19             for(int j=0;j<array[i].length;j++){
20                 total +=array[i][j];//成績彙總
21             }
22             System.out.println(str+"總成績:"+total);
23         }
24         
25        
26     }
27 }