英文:
How do I display the content of the void method java
问题
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]")) {
SortByView(StudNum, StudName, StudGrade);
} else if (str.matches("[sS]")) {
SortByStudNumber(StudNum, StudName, StudGrade);
} else if (str.matches("[Gg]")) {
SortByGrade(StudNum, StudName, StudGrade);
} else if (str.matches("[Ee]")) {
break;
}
} while (!TestInput(str));
}
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]);
}
}
public static void SortByStudNumber(int[] StudNum, String[] StudName, int[] StudGrade) {
//Sort by student number
for (int x = 0; x < StudNum.length; x++) {
for (int y = 0; y < StudNum.length - 1; y++) { // Fixed the typo here from x++ to y++
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]);
}
}
public static void SortByGrade(int[] StudNum, String[] StudName, int[] StudGrade) {
// sort by grade
for (int x = 0; x < StudNum.length; x++) {
for (int y = 0; y < StudNum.length - 1; y++) { // Fixed the typo here from x++ to y++
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]);
}
}
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;
}
}
英文:
I just want my program to display the sorted arrays in the void method but instead it doesn't display the sorted array or anything at all, it just loop around. I try removing the if else statement in the do while but it changes nothing. Anyone who's willing to help. It would be very much appreciated.
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, StudName, StudGrade);
} else if (str.matches("[Gg]")) {
SortByGrade(StudNum, StudName, 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, String[] StudName, 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, String[] StudName, 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```
答案1
得分: 3
以下循环递增是错误的
for(int y = 0 ; y < StudNum.length -1; x++)
修正后为
for(int y = 0 ; y < StudNum.length -1; y++)
输出:
学生人数 : 2
学生编号 : 1
学生姓名 : a
学生年级 : 1
学生编号 : 2
学生姓名 : b
学生年级 : 2
按什么排序? [S]学生编号 [G]年级 [V]查看 [E]退出 : s
1 a 1
2 b 2
英文:
Following loop increment is incorrect
for(int y = 0 ; y < StudNum.length -1; x++)
After fixing it to
for(int y = 0 ; y < StudNum.length -1; y++)
OUTPUT:
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
答案2
得分: 1
看起来对我来说,这应该被关闭,因为这是由一个拼写错误引起的。看一下你的其中一个for循环:
// 按学号排序
for (int x = 0; x < StudNum.length; x++) {
for (int y = 0; y < StudNum.length - 1; x++) { // < - 这里你增加了 x 而不是 y
}
}
你在所有的迭代中都做了同样的事情,你在 y
的for循环中增加了 x
。
英文:
Looks to me like this should be closed as was caused by a typo. Take a look at one of your for loops:
// 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
}
}
You do the same thing in all of your iterations, you are incrementing x
in your y
for.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论