3.从键盘读入学生成绩,找出最高分,并输出学生成绩等级。

  • 成绩>=最高分-10 等级为’A’
  • 成绩>=最高分-20 等级为’B’
  • 成绩>=最高分-30 等级为’C’
  • 其余 等级为’D’
public class ArrayTest {
    public static void main(String[] args) {
        
        System.out.print("请输入学生人数:");
        Scanner scann = new Scanner(System.in);
        int num = scann.nextInt();
        int[] result = new int[num];
        System.out.println("请输入" + num + "个成绩:");
        for(int i = 0; i < num; i++){
            int num2 = scann.nextInt();
            result[i] = num2;
        }
        int resultMax = result[0];
        for(int i = 1; i < num; i++){
            if(resultMax < result[i]){ 
                resultMax = result[i];
            }
        }
        System.out.println("最高分为:" + resultMax);
        for(int i = 0; i < num; i++){
            if(result[i] >= resultMax - 10){
                System.out.println("student " + i + " score is " + result[i] + " grade is A" );
            }else if(result[i] >= resultMax - 20){
                System.out.println("student " + i + " score is " + result[i] + " grade is C" );
            }else if(result[i] >= resultMax - 30){
                System.out.println("student " + i + " score is " + result[i] + " grade is B" );
            }else{
                System.out.println("student " + i + " score is " + result[i] + " grade is D" );
            }
        }
    }
}

4.获取arr数组中所有元素的和。
20210722173217.jpg

public class ArrayTest02 {
    public static void main(String[] args) {
        
        int[][] arr = new int[3][];
        
        /**
        arr[0] = new int[3];
        arr[0][0] = 3;
        */
        
        arr[0] = new int[]{3,5,8};
        arr[1] = new int[]{12,9};
        arr[2] = new int[]{7,0,6,4};
        int num = 0; 
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i].length; j++){
                num += arr[i][j];
            }
        }
        System.out.println(num);
    }
}

5.使用二维数组打印一个 10 行杨辉三角。

public class ArrayTest03 {
    public static void main(String[] args) {
        int[][] yangHui = new int[10][];
        for(int i = 0; i < yangHui.length; i++){
            yangHui[i] = new int[i + 1];
            
            yangHui[i][0] = 1;
            yangHui[i][i] = 1;
            
            if(i > 1){
                for(int j = 1; j < yangHui[i].length - 1; j++){
                    yangHui[i][j] = yangHui[i-1][j-1] + yangHui[i-1][j];
                }
            }
        }
        for(int i = 0; i < yangHui.length; i++){
            for(int j = 0; j < yangHui[i].length; j++){
                System.out.print(yangHui[i][j] + "  ");
            }
            System.out.println();
        }
    }
}
最后修改:2021 年 07 月 22 日 05 : 40 PM
如果觉得我的文章对你有用,请随意赞赏