老师,还有其他的解法可以参考吗?

来源:1-8 编程练习

ExcelYourself

2020-08-25 15:20:39

import java.util.Scanner;

public class ScoreDemo{

   public static void main(String[] args){

       //定义一个三行两列的整型二维数组intArray

       int[][] intArray = new int[3][2];

       int sum1 = 0; int sum2 = 0;

       //从键盘输入学生成绩,要求输入顺序与效果图一致

       Scanner sc = new Scanner(System.in);

       for (int i = 0; i < intArray.length; i++) {

           for(int j = 0; j < intArray[i].length -1 ; j++) {

           System.out.println("The No. " + (i+1) + " student's Chinese result is :  ");

           intArray[i][j] = sc.nextInt();

           sum1 += intArray[i][j];

           System.out.println("The No. " + (i+1) + " student's Maths result is :  ");

           intArray[i][j] = sc.nextInt();

           sum2 += intArray[i][j];

           }

       }

       

       int chinese_mean = sum1 / intArray.length;

       int maths_mean = sum2 / intArray.length;

       //求语文的总成绩和平均分

       System.out.println("The sum of Chinese results is :  " + sum1);

       System.out.println("The mean of Chinese results is :  " + chinese_mean);

       //求数学的总成绩和平均分

       System.out.println("The sum of Maths results is :  " + sum2);

       System.out.println("The mean of Maths results is :  " + maths_mean);

   }

}


写回答

1回答

好帮手慕阿慧

2020-08-25

同学你好,同学的代码完成的不错,很棒!同学代码有以下几处可以优化:

1、在第二层for循环中,j最初是0,intArray[i].length为2,那么j<intArray[i].length-1,

就是j<1。所以第二层for循环的内容只会执行一次,同学可以去掉第二层for循环。

2、同学可以使用二维数组中的第一列存放语文成绩,第二列存放数学成绩。

参考代码如下:

http://img.mukewang.com/climg/5f44c9ad09465dac07000252.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

0 学习 · 16556 问题

查看课程