我正在开发一个Java项目,该项目请求用户输入信息,如名称,ID,数组中的得分。我需要帮助计算用户输入的平均成绩以及如何找出得分最高的人。这是我的代码:
package finalproject;
import java.util.Scanner;
public class FinalProject {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Cis84[] student = new Cis84[50];
int option;
for (int c = 0; c < 50; c++)
student[c] = new Cis84();
do {
System.out.print("");
System.out.println("1) Add Information");
System.out.println("2) Show report");
System.out.println("3) Exit");
System.out.print("\nEnter option: ");
option = input.nextInt();
switch (option) {
case 1:
String n;
double g;
int index,
i;
System.out.println("Which position of the student?");
index = input.nextInt();
System.out.println("What is the student's name:");
n = input.nextLine();
n = input.nextLine();
System.out.println("What is student's Id");
i = input.nextInt();
System.out.println("What is student's score");
g = input.nextDouble();
student[index].setName(n);
student[index].setGrade(g);
student[index].setId(i);
break;
case 2:
for (int c = 0; c < 50; c++)
System.out.println(student[c]);
break;
case 3:
System.out.println("You are done");
break;
default:
System.out.println("Try again");
break;
}
} while (option != 3);
}
}
和班级
package finalproject;
public class Cis84 {
private String name;
private int id;
private double grade;
public Cis84() {
name = "not input yet";
id = 00000;
grade = 0.0;
}
public Cis84(String n, int i, double g) {
name = n;
id = i;
grade = g;
}
public void setName(String n) {
name = n;
}
public void setId(int i) {
id = i;
}
public void setGrade(double g) {
grade = g;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public double getGrade() {
return grade;
}
public String toString() {
return String.format("%s\n%d\n%.2f\n", name, id, grade);
}
}
答案 0 :(得分:2)
很明显,这是为了完成家庭作业,我觉得给你一个直接的答案感觉不舒服。但是,您要做的是在显示平均值时,遍历整个students
数组并总结得分,然后除以数组的大小,可能使用for循环。您可以通过在调用switch-case的选项1时随时增加计数器来跟踪数组的大小。
要找到最高分,您应该能够使用上面提到的平均值计算循环,并检查等级与上一个最高等级。记录最高等级的索引并将其打印出来。
有一些伪代码!
for(size of array which isn't NULL){
add indexed grade to sum;
check to see if this index has the highest grade;
}
display (sum/size); //the average
display highest grade;
答案 1 :(得分:0)
计算平均值将类似于下面的代码。请记住,平均值是所有值之和除以值总数
double sum = 0, divisor = 0;
for (int k = 0; k < student.length; k++){
sum += student[k].getGrade();//add up all the grades
divisor = k;//get the number of total items
}
return sum/divisor; //divide
答案 2 :(得分:0)
private static double calculateAverage(Cis84[] students) {
double sum = 0;
for (Cis84 student : students) {
sum += student.getGrade();
}
return sum / students.length;
}
private static double calculateHighestScore(Cis84[] students) {
double highestScore = 0;
for (Cis84 student : students) {
if (student.getGrade() > highestScore) {
highestScore = student.getGrade();
}
}
return highestScore;
}
然后,显示信息:
case 2:
System.out.println("Average:");
System.out.println(calculateAverage(student));
System.out.println("Highest score:");
System.out.println(calculateHighestScore(student));