gpt4 book ai didi

java - 我如何让这个数组结束?

转载 作者:行者123 更新时间:2023-11-29 07:19:55 24 4
gpt4 key购买 nike

嘿,所以我有这个硬件作业,我必须编写一个程序,允许用户输入学生姓名和成绩,并返回最高最低和平均水平。唯一的问题是类最多有 50 名学生(或者表格上说的),而且上面只有 10 个学生的名字和成绩。我如何让数组在最后一个学生之后结束?

static final int MAX = 50;
public static void main(String[] args)
{
int index, average;
int highest = 0, student = 1;
Scanner keyboard = new Scanner(System.in);
String [] students = new String[MAX];
int[] grades = new int[MAX];

System.out.println("Please enter the name of the student and their test grade");
System.out.print("Students: ");
for(index=0; index<MAX; index++)
{
students[index] = keyboard.nextLine();
student = student + index;
if(students[index]=="Done"))
{
student = student - 1;
continue;
}

}
System.out.print("Grades: ");
for(index=0; index<student; index++)
grades[index] = keyboard.nextInt();
averageMan(grades, student);
for(index=0; index<student; index++)
if(grades[index] < averageMan(grades, student))
System.out.println(students[index] + " Your test grade was " +
"below the class average. Step it up.");
for(index=0; index<student; index++)
if(grades[index] > highest)
highest = grades[index];
System.out.println("The highest test grade in the class goes to " +
students[highest] + " with a grade of " + grades[highest]);
}

public static int averageMan(int[] g, int s)
{
int index, sum = 0;
int averages;
for(index=0; index<s; index++)
sum = sum + g[index];
averages = sum / s;
return averages;
}

最佳答案

使用集合,而不是数组。他们的 api 更有用。

阅读Collections Trail开始。

如果您想直接替换数组,use a List ,如果你想要唯一性,use a Set .

关于java - 我如何让这个数组结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5912708/

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