猿问
下载APP

java二维数组的使用?

录入学生考试多门课程后的成绩。输入多门课程的名称,输入参加考试这门课程的人数,录入他们考试的成绩,然后按下面图中的样式输出。http://img.mukewang.com/5e94330300011bc510220295.jpg

申涂
浏览 234回答 1
1回答

KeepLearning_XU

package temptest;import java.util.Scanner;public class Test7 { public static void main(String[] args) { int peopleNumber; Scanner sc = new Scanner(System.in); System.out.println("请输入参加考试的人数:"); peopleNumber = sc.nextInt(); String[] peopleName = new String[peopleNumber]; System.out.println("请输入考生姓名:"); for (int i = 0; i < peopleName.length; i++) { peopleName[i] = sc.next(); } int courseNumber; System.out.println("请输入考试门数:"); courseNumber = sc.nextInt(); String[] courseName = new String[courseNumber]; System.out.println("请输入考试课程名称:"); for (int i = 0; i < courseName.length; i++) { courseName[i] = sc.next(); } int[][] grade = new int[peopleNumber][courseNumber]; for (int i = 0; i < grade.length; i++) { System.out.println("请输入考生" + peopleName[i] + "的考试成绩:"); for (int j = 0; j < grade[i].length; j++) { System.out.println("请输入" + courseName[j] + "的考试成绩:"); grade[i][j] = sc.nextInt(); } } int[] gradeSum = new int[courseNumber]; for (int i = 0; i < gradeSum.length; i++) { for (int j = 0; j < courseNumber; j++) { gradeSum[i] += grade[i][j]; } } int[] gradeAve = new int[courseNumber]; for (int i = 0; i < gradeAve.length; i++) { gradeAve[i] = gradeSum[i] / courseNumber; } System.out.println(); System.out.println("显示如下:"); System.out.print("学生:\t"); for (int i = 0; i < courseName.length; i++) { System.out.print(courseName[i] + "\t\t"); } System.out.println("总分:\t平均分"); for (int i = 0; i < peopleNumber; i++) { System.out.print(peopleName[i] + "\t\t"); for (int j = 0; j < courseNumber; j++) { System.out.print(grade[i][j] + "\t\t"); } System.out.println(gradeSum[i] + "\t\t" + gradeAve[i]); } }}
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答