【发布时间】:2021-02-28 05:06:35
【问题描述】:
最终输出将显示谁的成绩最高,谁的成绩最低。 我不知道如何将最低名称/等级称为最终输出。
在代码中,我有一些 cmets 在我遇到“currentMinIndex”的地方,“currentMaxIndex”工作得很好,并将其显示到最终输出。我试图反映它,但它并没有像我预期的那样发展。不确定“(int k = 1; k>= m; k++)”是否不正确。
import java.util.*;
public class MyArrayEX {
// Sort grades lowest on top
public static int[] reverseInt(int[] array) {
int[] input = new int[array.length];
for (int i = 0, j = input.length - 1; i < array.length; i++, j--) {
input[j] = array[i];
}
return input;
}
public static void main(String[] args) {
// Scanners
Scanner input = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
// Input amount
System.out.print("\nEnter number of students: ");
int numOfStu = input.nextInt(); // Number of Students
int[] grades = new int[numOfStu];
String[] names = new String[numOfStu];
// Start loop, amount is based off of "numOfStu"
for (int i = 0; i < numOfStu; i++) {
System.out.print("\rEnter student first name: ");
String name = keyboard.next();
System.out.print("Enter the students grade: ");
int grade = input.nextInt();
// Assigning i
names[i] = name;
grades[i] = grade;
//System.out.println("");
}
// This is the area that sorts it from least to greatest
// i is the indexed value of the last number in array
for (int i = grades.length - 1; i > 0; i--) {
// Resets both to 0 to start at the beginning of the array
int currentMax = grades[0];
int currentMaxIndex = 0;
// i is back-limit that gets chopped off by one each time
for (int k = 1; k <= i; k++) {
if (currentMax < grades[k]) {
currentMax = grades[k];
currentMaxIndex = k;
}
}
// This is where im lost on how to call the min value
// Trying to mirror the one above but using it
// to show the minimum grade along with the name
for (int m = grades.length - 1; i > 0; i--) {
int currentMin = grades[0];
int currentMinIndex = 0;
// Min grades
for (int k = 1; k >= m; k++) {
if (currentMin < grades[m]) {
currentMin = grades[m];
currentMinIndex = m;
}
}
// After largest number is found, assign that number to i
// Im trying to have the final output show the min/max grade with who has it
// Would the MinIndex be assigned to a different variable?
grades[currentMaxIndex] = grades[i];
grades[currentMinIndex] = grades[m];
grades[i] = currentMax;
grades[m] = currentMin;
String highName = names[currentMaxIndex];
String lowName = names[currentMinIndex];
names[currentMaxIndex] = names[i];
names[currentMinIndex] = names[m];
names[i] = highName;
names[m] = lowName;
// This shows the name and grade for the highest number
System.out.print("\rThe highest grade is " + highName + " with a " + currentMax);
// Unsure how to call this.
System.out.println("\r and the Lowest grade is " + lowName + " with a " + currentMin);
}
}
input.close();
keyboard.close();
}
}
【问题讨论】:
-
您对最低成绩的测试太与最高成绩的测试完全一样。
currentMin<grades[m]。这是错误的方法;你想要grades]m] < currentMin或currentMin >= grades[m]。 -
当你只想要数组中的最高和最低时,你为什么要对它们进行排序
-
@BeshambherChaukhwan 不要相信你在 cmets (;->) 中读到的所有内容;代码讲述了真实的故事。他实际上是在“搜索”,而不是真正的“分类”。
-
是的,代码真的很混乱,比如为什么循环中的循环以及为什么有 2 个扫描仪用于相同的输入流。当他只想要一个输出时,他不会为最高最低数据打印多行吗?
-
这是一个非常令人困惑的代码。您可以在单个循环中找到最大和最小元素。为什么使用
two nested loops?