嗨,所以我正在制作一个程序,使用数组和方法计算10个学生成绩,学生的分数如下所示。我遇到的问题是显示名称平均值和3个列中的等级彼此相邻。用于打印数组的当前代码打印出来就像这样
The Average is
Average: 82.4
Average: 81.2
Average: 66.6
Average: 72.2
Average: 52.2
Average: 59.2
Average: 63.4
Average: 90.0
Average: 73.4
Average: 68.8
The grade is
Grade = B
Grade = B
Grade = C
Grade = C
Grade = D
Grade = D
Grade = D
Grade = A
Grade = C
Grade = C
BUILD SUCCESSFUL (total time: 0 seconds)
这里是带有名称标记和平均值的代码我已经做过任何帮助非常感谢
import java.util.Arrays;
public class Question2Assignment {
public static double [][] Marks;
public static char [] graded;
public static String [] Name;
public static double [] avg;
public static void main(String[] args) {
inMarks() ;
SNames();
System.out.println("The Average is");
for (double number : AverageMarks(Marks))
{
System.out.print("Average: " + number);
System.out.println("");
}
System.out.println("The grade is");
for (char grade : Grade(graded))
{
System.out.print("Grade = " + grade);
System.out.println("");
}
}
private static void inMarks()
{
Marks = new double [][] {{85,83,77,91,76},{80,90,95,93,48},{78,81,11,90,73},{92,83,30,69,87},{23,45,96,38,59},{60,85,45,39,67},{77,31,52,74,83},{93,94,89,77,97},{79,85,28,93,82},{85,72,49,75,63}};
}
private static void SNames()
{
Name = new String [] {"Jognson","Aniston","Cooper","Gupta","Blair","Clark","Kennedy","Bronson","Sunny","Smith"};
}
public static double[] AverageMarks(double[][] Marks)
{
double avg[] = new double [Marks.length];
for (int row = 0; row < Marks.length; row++){
int sum=0;
for (int col = 0; col < Marks[row].length; col++){
sum += Marks[row][col];
}
avg[row] = 1.0*sum / Marks[row].length; // calc average
}
return avg;
}
public static char [] Grade(char[] Graded)
{
char [] graded = new char [10];
int count = 0;
for (double number : AverageMarks(Marks))
{
if(number <50)
{
graded [count] = 'F';
count++;
}
else if (number <65)
{
graded [count]= 'D';
count++;
}
else if (number <75)
{
graded [count] = 'C';
count++;
}
else if (number <85)
{
graded [count] ='B';
count++;
}
else
{
graded [count]='A';
count++;
}
}//first for loop
return graded;
}
}//class end
答案 0 :(得分:2)
for (int i = 0; i<Name.length; i++) {
System.out.println(Name[i]+" "+avg[i]+" "+graded[i]);
}
答案 1 :(得分:0)
`double[] averageMarks = AverageMarks(Marks);
char[] grades = Grade(graded);
for (int i = 0; i<Name.length; i++) {
System.out.println(Name[i]+" "+averageMarks[i]+" "+grades[i]);
}`
上面给出的答案指出了正确的方向,但是对于这种情况给出了错误的结果