gpt4 book ai didi

java - 尝试使用三个数组Java获得三列数据

转载 作者:行者123 更新时间:2023-12-02 03:02:47 25 4
gpt4 key购买 nike

您好,我正在开发一个程序,使用数组和方法计算 10 名学生的成绩,学生的分数如下所示。我遇到的问题是在彼此相邻的三列中显示姓名平均值和成绩。我当前用来打印数组的代码像这样打印它们

 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

最佳答案

for (int i = 0; i<Name.length; i++) {
System.out.println(Name[i]+" "+avg[i]+" "+graded[i]);
}

关于java - 尝试使用三个数组Java获得三列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42198100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com