学生成绩的 JAVA 数组对[关闭]
Posted
技术标签:
【中文标题】学生成绩的 JAVA 数组对[关闭]【英文标题】:JAVA array pair for student grades [closed] 【发布时间】:2021-05-31 07:06:36 【问题描述】:如何将学生数组与成绩数组配对?当我找到最高成绩时,相应的学生也应该显示,与最低成绩的学生相同。我不知道如何让这个程序使用两个单独的数组来执行。
import java.util.Scanner;
公共类 Asm7
public static void main(String[] args)
Scanner Scan = new Scanner(System.in);
System.out.println("How many students do you have?: ");
int AMOUNT = 0;
AMOUNT = Scan.nextInt();
String[] STUDENT = new String [AMOUNT];
int COUNTER = 0;
int GRADE [] = new int [AMOUNT];
if (AMOUNT <= 0)
System.out.println("Invalid student amount");
else
for(int i = 0; i < AMOUNT; i++)
System.out.println("Enter student's first name: " + (i+1));
STUDENT[i] = Scan.next();
System.out.println("Enter student's grade in order added: ");
GRADE[i] = Scan.nextInt();
for(int i = 0; i < AMOUNT; i++)
System.out.println(STUDENT[i] + " received the final grade of " + GRADE[i]);
System.out.println();
int [] Results = MinMax(GRADE);
System.out.println("The highest grade in the class was " + Results[1]);
System.out.println("The lowest grade in the class was "+ Results[0]);
public static int[] MinMax(int[] value)
int[] Result = new int[]Integer.MAX_VALUE, Integer.MIN_VALUE;
for (int i : value)
Result[0] = i < Result[0] ? i : Result[0];
Result[1] = i > Result[1] ? i : Result[1];
return Result;
【问题讨论】:
您的代码有很多问题。 MinGrade 和 MaxGrade 看起来都不正确。您的最低和最高等级的打印输出也不正确 - 您应该在循环之后打印出最小的,而不是在每次迭代中。从做对一件事开始! 【参考方案1】:您对学生人数的while
循环验证有点晚了。您希望在声明和初始化数组之前执行此操作。然而,while
循环实际上被用于尝试某种形式的验证这一事实是一个非常好的迹象。这比大多数新程序员倾向于做的要多。所有输入都应经过验证,并为用户提供提供正确解决方案的机会。这只会为用户带来更流畅、无故障的应用程序和更好的体验。看看你的代码中的这个 while
循环:
while (amount < 0)
System.out.println("Invalid student amount");
如果用户提供-1
(这是+1 的有效整数值)会发生什么?没错...您的应用程序将进入无限循环,将Invalid student amount
输出到控制台窗口。您的验证方案应该包含整个提示,然后应该更合理地定义退出它的方法。对于while
循环,最好的退出是通过其条件语句完成,如果条件为假则退出循环,例如:
Scanner scan = new Scanner(System.in);
// Number Of Students...
String inputString = "";
while (inputString.isEmpty())
System.out.print("How many students do you have?: --> ");
inputString = scan.nextLine().trim();
/* Is the supplied Number Of Students valid and within
range (1 to 50 inclusive)? */
if (!inputString.matches("\\d+") || Integer.valueOf(inputString) < 1
|| Integer.valueOf(inputString) > 50)
// No...
System.err.println("Invalid entry (" + inputString + ") for Student "
+ "amount! Try again...");
inputString = ""; // Empty inputString so we loop again.
System.out.println();
// Valid amount provided.
int amount = Integer.valueOf(inputString);
String[] student = new String[amount];
int grade[] = new int[amount];
您会立即注意到这里有一些明显的变化。整个How many students do you have?
提示包含在while
循环块中。如果用户未提供有效响应,则要求该用户重试。 student
和 grade
parallel arrays 仅在为学生人数提供有效响应后声明和初始化。
您还会注意到while
循环条件不依赖于整数值,而是依赖于实际的字符串内容(不管它是什么)。如果变量为空(“”),则再次循环。这是因为Scanner#nextLine() 方法用于收集用户输入,而不是Scanner#nextInt() 方法。提示仍然需要提供一个整数值,只是一个整数值的字符串表示形式,并且使用String#matches() 方法和一个小的Regular Expression(正则表达式)来验证。
出于多种原因,我个人更喜欢使用Scanner#nextLine() 方法。我个人觉得它更灵活,特别是如果您想从单个提示中同时接受 Alpha 和数字输入。如果上面的提示是:
How many students do you have? (q to quit)
您只需在数字验证代码上方添加另一个if
语句,以查看是否提供了“q”或“Q”,例如:
// If either q or Q is entered then quit application.
if (amountString.matches("[qQ]"))
System.out.println("Bye-Bye");
System.exit(0);
另外,通过将一个好的表达式传递给matches()方法,不需要为了进行验证而捕获异常,并不是说这有什么问题,很多人都这样做,我特别不但是当我不需要这样做时。
旁注:我要在这里陈述显而易见的事情,我相信你已经听过一百次了,你已经厌倦了,但我要再告诉你一次:
您的类方法应该以小写字母开头(请参阅Java Naming Conventions)。 我知道你没有听到编译器抱怨,但它确实使它成为 (有时)更难阅读代码。每个读书的人 你的代码会感谢你的。
因为student
和grade
数组是parallel arrays,您可能希望minGrade() 和maxGrade() 方法返回特定的数组索引值到最低或最高等级,以便可以对包含确定的特定等级的学生建立参考关系。所以,这会更有用:
public static int minGrade(int[] arr, int size)
// Initialize min to have the highest possible value.
int min = Integer.MAX_VALUE;
int returnableIndex = -1;
// loop to find lowest grade in array
for (int i = 0; i < arr.length; i++)
if (arr[i] < min)
min = arr[i];
returnableIndex = i;
return returnableIndex;
public static int maxGrade(int[] arr, int size)
int max = Integer.MIN_VALUE;
int returnableIndex = -1;
// loop to find highest grade in array
for (int i = 0; i < size; i++)
if (arr[i] > max)
max = arr[i];
returnableIndex = i;
return returnableIndex;
所有内容都在运行,您的代码可能如下所示:
public static void main(String[] args)
Scanner scan = new Scanner(System.in);
// Number Of Students...
String amountString = "";
while (amountString.isEmpty())
System.out.print("How many students do you have?: --> ");
amountString = scan.nextLine().trim();
// Is the supplied Number Of Students valid and within
// range (1 to 50 inclusive)?
if (!amountString.matches("\\d+") || Integer.valueOf(amountString) < 1
|| Integer.valueOf(amountString) > 50)
// No...
System.err.println("Invalid entry (" + amountString + ") for Student "
+ "amount! Try again...");
amountString = ""; // Empty inputString so we loop again.
System.out.println();
// Valid amount provided.
int amount = Integer.valueOf(amountString);
// Declare and initialize parallel arrays
String[] student = new String[amount];
int grade[] = new int[amount];
// Student Names and Grade...
for (int i = 0; i < amount; i++)
// Student Name...
String name = "";
while (name.isEmpty())
System.out.print("Enter student #" + (i + 1) + " name: --> ");
name = scan.nextLine().trim();
/* Is the name valid (contains upper or lower case letters from
A-Z and a single whitespaces separating first and last name?
Whitespace and last name is optional. */
if (!name.matches("(?i)([a-z]+)(\\s1)?([a-z]+)?"))
// No..
System.err.println("Invalid Student #" + (i + 1) + " name ("
+ name + ")! Try Again...");
System.out.println();
name = ""; // Empty name so we loop again.
// Valid Student name provided...
student[i] = name;
// Student Grade...
String gradeString = "";
while (gradeString.isEmpty())
System.out.print("Enter student #" + (i + 1) + " grade: --> ");
gradeString = scan.nextLine().trim();
// Is the supplied grade valid and within range (0 to 100 inclusive)?
if (!gradeString.matches("\\d+")
|| Integer.valueOf(gradeString) < 0
|| Integer.valueOf(gradeString) > 100)
// No...
System.err.println("Invalid entry (" + gradeString + ") for "
+ "Student #" + (i + 1) + " grade! Try again...");
gradeString = "";
System.out.println();
// Valid Student grade provided...
grade[i] = Integer.valueOf(gradeString);
// Display everyone's grade
System.out.println();
for (int i = 0; i < amount; i++)
System.out.println(student[i] + " received the final grade of " + grade[i]);
System.out.println();
//Display who is highest and lowest...
int index = maxGrade(grade, amount);
System.out.println("The highest grade in the class was by '" + student[index]
+ "' with a grade of: " + grade[index]);
index = minGrade(grade, amount);
System.out.println("The lowest grade in the class was by '" + student[index]
+ "' with a grade of: " + grade[index]);
public static int minGrade(int[] arr, int size)
// Initialize min to have the highest possible value.
int min = Integer.MAX_VALUE;
int returnableIndex = -1;
// loop to find lowest grade in array
for (int i = 0; i < arr.length; i++)
if (arr[i] < min)
min = arr[i];
returnableIndex = i;
return returnableIndex;
public static int maxGrade(int[] arr, int size)
int max = Integer.MIN_VALUE;
int returnableIndex = -1;
// loop to find highest grade in array
for (int i = 0; i < size; i++)
if (arr[i] > max)
max = arr[i];
returnableIndex = i;
return returnableIndex;
【讨论】:
【参考方案2】:如果数据没有排序,最好在打印学生和他们的成绩后,在同一个循环中找到min
和max
的成绩。
那么就不需要循环打印min
和max
成绩了:
for (int i = 0; i < amount; i++)
System.out.println(student[i] + " received the final grade of " + grade[i]);
int min = grade[0];
int max = grade[0];
for (int i = 1; i < amount; i++)
if (grade[i] < min)
min = grade[i];
else if (grade[i] > max)
max = grade[i];
System.out.println("The highest grade in the class was " + max);
System.out.println("The lowest grade in the class was " + min);
如果要查找 min/max 的索引,则可以打印获得 min 和 max 成绩的学生的姓名。
【讨论】:
我将如何在方法中执行此操作? 使方法返回int
值(分别为最小值或最大值)而不是void
太棒了,您知道如何将学生姓名与成绩配对,因此它不仅显示最高成绩和最低成绩,还显示相应的最高和最低成绩学生【参考方案3】:
public static void main(String[] args)
int[] grades = new int[]50, 51, 52, 50, 60, 22, 53, 70, 60, 94, 56, 41;
int[] result = getMinMax(grades);
System.out.println("Min: " + result[0] + ", Max: " + result[1]);
public static int[] getMinMax(int[] values)
int[] result = new int[]Integer.MAX_VALUE, Integer.MIN_VALUE;
for (int i : values)
result[0] = i < result[0] ? i : result[0];
result[1] = i > result[1] ? i : result[1];
return result;
您需要处理 int[] 值为 null 或空的情况。您可以决定(抛出异常,返回 null... 或其他)
【讨论】:
感谢这个特殊的分配,最小值和最大值必须通过一个方法和 for 循环 更新答案。 非常感谢您的工作和更新,现在您也知道如何将名称与成绩配对了吗? 是的。修改 getMinMax 方法以返回最小值和最大值 INDEX(不是值)。然后,您可以使用 Studen[] 和 Grade[] 数组的最小和最大索引值来获取名称和等级。以上是关于学生成绩的 JAVA 数组对[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
利用JAVA编写程序,用一维数组保存20个学生的某门课程的成绩,计算平均成绩,并输出。