问题描述
我只希望我的程序以void方法显示排序后的数组,但是它根本不显示排序后的数组或其他任何内容,而是循环播放。我尝试在do while中删除if else语句,但是它什么也没有改变。任何愿意帮助的人。非常感谢。
public class ArraysSorting {
public static void main(String[] args) {
Scanner in = new Scanner(system.in);
System.out.print("Number of Students : ");
int num = in .nextInt();
int[] StudNum = new int[num];
String[] StudName = new String[num];
int[] StudGrade = new int[num];
for (int x = 0; x < num; x++) {
System.out.print("Student Number : ");
StudNum[x] = in .nextInt(); in .nextLine();
System.out.print("Student Name : ");
StudName[x] = in .nextLine();
System.out.print("Student Grade : ");
StudGrade[x] = in .nextInt(); in .nextLine();
System.out.println();
}
String str;
do {
System.out.print("\nSort By ? [S]Student Number [G]Grade [V]View [E]Exit : ");
str = in .nextLine();
if (str.matches("[vV]")) // i try removing this but nothing happens
{
SortByView(StudNum,StudName,StudGrade);
} else if (str.matches("[sS]")) {
SortByStudNumber(StudNum,StudGrade);
} else if (str.matches("[Gg]")) {
SortByGrade(StudNum,StudGrade);
} else if (str.matches("[Ee]")) {
break;
}
} while (!(TestInput(str)));
} //end of main method
public static void SortByView(int[] StudNum,String[] StudName,int[] StudGrade) {
//Sort by view
for (int x = 0; x < StudNum.length; x++) {
System.out.print("\n" + StudNum[x] + " " + StudName[x] + " " + StudGrade[x]); // pls display this output
}
}
public static void SortByStudNumber(int[] StudNum,int[] StudGrade) {
//Sort by student number
for (int x = 0; x < StudNum.length; x++) {
for (int y = 0; y < StudNum.length - 1; x++) {
if (StudNum[y] > StudNum[y + 1]) {
int temp = StudNum[y];
StudNum[y] = StudNum[y + 1];
StudNum[y + 1] = temp;
String temp1 = StudName[y];
StudName[y] = StudName[y + 1];
StudName[y + 1] = temp1;
int temp2 = StudGrade[y];
StudGrade[y] = StudGrade[y + 1];
StudGrade[y + 1] = temp2;
}
}
}
for (int x = 0; x < StudNum.length; x++) {
System.out.print("\n" + StudNum[x] + " " + StudName[x] + " " + StudGrade[x]); // pls display this output
}
}
public static void SortByGrade(int[] StudNum,int[] StudGrade) {
// sort by grade
for (int x = 0; x < StudNum.length; x++) {
for (int y = 0; y < StudNum.length - 1; x++) {
if (StudGrade[y] > StudGrade[y + 1]) {
int temp = StudNum[y];
StudNum[y] = StudNum[y + 1];
StudNum[y + 1] = temp;
String temp1 = StudName[y];
StudName[y] = StudName[y + 1];
StudName[y + 1] = temp1;
int temp2 = StudGrade[y];
StudGrade[y] = StudGrade[y + 1];
StudGrade[y + 1] = temp2;
}
}
}
for (int x = 0; x < StudNum.length; x++) {
System.out.print("\n" + StudNum[x] + " " + StudName[x] + " " + StudGrade[x]); // pls display this output
}
}
public static boolean isExit(String input) {
return input.matches("[eE]") && input.length() == 1;
}
public static boolean TestInput(String input) {
return input.matches("[sSvVgGeE]") && input.length() == 1;
}
} //end of class```
解决方法
以下循环增量不正确
for(int y = 0 ; y < StudNum.length -1; x++)
修复后
for(int y = 0 ; y < StudNum.length -1; y++)
输出:
Number of Students : 2
Student Number : 1
Student Name : a
Student Grade : 1
Student Number : 2
Student Name : b
Student Grade : 2
Sort By ? [S]Student Number [G]Grade [V]View [E]Exit : s
1 a 1
2 b 2
,
在我看来,这应该是由于错字造成的。看看您的其中一个for循环:
// Sort by student number
for (int x = 0; x < StudNum.length; x++) {
for (int y = 0; y < StudNum.length - 1; x++) { // <- Here you increment x instead of y
}
}
您在所有迭代中都做同样的事情,您要在x
中增加y
。