以下是该计划的要求。基本上输出必须如下,同时仍然遵循以下指南。我的问题是如何改变我的程序以满足要求#4(正在编写一种方法,将学生数据记录在1-D和2-D阵列中并打印学生记录:名称,2个测验分数和平均值。输出字段应该是良好对齐的。)我的第二个问题是如何满足要求#6(在学生记录之前应该打印标题行,并且“------------- ------“每2名学生应该打印一次”。
# Name Q1 Q2
name1 ...... ......
name2 ...... ......
name3 ...... ......
name4 ...... ......
=============================================== =================================== 1.在main()方法内,定义一个可以存储5个学生名字的1-D数组。在main()中定义一个二维数组,用于存储5个学生(行)考试记录,每个学生有3个数据字段(列):2个测验分数和2个测验的平均值。
将学生姓名(真实姓名)分配给1-D阵列,将2个测验分数分配给2-D阵列。
计算每个学生的2个测验的平均值。
4。编写一个获取学生数据(1-D和2-D数组)的方法,并按平均字段将学生记录从高到低排序。
6。学生记录前应打印标题行,每2名学生应打印一行“-------------------”。
name1 ...... ......
name2 ...... ......
name3 ...... ......
name4 ...... ......
=============================================== ==================================
这是我到目前为止所拥有的。我真的遇到排序方法的麻烦,每两名学生打印一行“-------------------”。
公共班学生同志{
public static void main(String[] args) {
//create variables to be used inside the "for" loops as counters
int i;
int row;
int column;
String[] students = {"Peter", "Lydia", "Kate", "Steve", "Alexa"};
// Create a 2-D array of the int type to stores scores
int[][] scores = { {82, 90}, {78,90}, {84, 80}, {85, 73}, {81, 93} };
// Display headings for information to be spaced equally
System.out.printf("%-7s%-7s%-7s%-7s%-7s", "Name",
"Test1", "Test2", "Sum of tests ", "Average grade\n");
System.out.print("----------------------------------------\n");
for (i = 0; i <students.length; i++){
System.out.print(students[i] + " \n");
}
// cycle through the each group (outside part of the array)
for (row=0; row<scores.length; row++){
//create variables to store computations and set initial value to zero
int sum = 0;
double avg = 0;
//System.out.print(students[i]);
// cycle through each set of elements of each group (inside part of array)
for (column=0; column<scores[row].length; column++){
// for each set of elements -- add all elements in each group (int sum must be set to 0 each time)
sum += scores[row][column];
// calculate average by dividing the value in int sum by the number of elements in the group (group size)
avg = sum/scores[column].length;
// display the values of each row
System.out.print(scores[row][column] + " ");
}
// display the sum of each group (row) with an identifying message
System.out.print(sum + " " );
// display the average of each group (row) with an identifying message
System.out.print(avg);
// System.out.print(" -------------------------\n");
// create new line after each computation
System.out.println();
// Create dotted lines to separate each set of values displayed
System.out.print("----------------------------------------\n");
}
///////
} }
答案 0 :(得分:-1)
package Examples;
public class MarksQuiz {
public static void main(String[] args) {
// create variables to be used inside the "for" loops as counters
int i;
int row;
int column;
String[] students = { "Peter", "Lydia", "Kate", "Steve", "Alexa" };
// Create a 2-D array of the int type to stores scores
int[][] scores = { { 82, 90 }, { 78, 90 }, { 84, 80 }, { 85, 73 }, { 81, 93 } };
// Display headings for information to be spaced equally
System.out.printf("%-7s%-7s%-7s%-7s%-7s", "Name", "Test1", "Test2", "Sum of tests ", "Average grade\n");
System.out.print("----------------------------------------\n");
//Removed the For loop from Here
/*for (i = 0; i < students.length; i++) {
System.out.print(students[i] + " \n");
}*/
// cycle through the each group (outside part of the array)
for (row = 0; row < scores.length; row++) {
System.out.print(students[row]);//Inserted the Print Statement to Fix your Issue
// create variables to store computations and set initial value to
// zero
int sum = 0;
double avg = 0;
// System.out.print(students[i]);
// cycle through each set of elements of each group (inside part of
// array)
for (column = 0; column < scores[row].length; column++) {
// for each set of elements -- add all elements in each group
// (int sum must be set to 0 each time)
sum += scores[row][column];
// calculate average by dividing the value in int sum by the
// number of elements in the group (group size)
avg = sum / scores[column].length;
// display the values of each row
System.out.print(scores[row][column] + " ");
}
// display the sum of each group (row) with an identifying message
System.out.print(sum + " ");
// display the average of each group (row) with an identifying
// message
System.out.print(avg);
// System.out.print(" -------------------------\n");
// create new line after each computation
System.out.println();
// Create dotted lines to separate each set of values displayed
System.out.print("----------------------------------------\n");
}
}
}
输出: -