如何显示void方法的内容java

How do I display the content of the void method java

我只希望我的程序在 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, 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```

在我看来,这应该被关闭,因为打字错误。看看你的一个 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

  }
}

您在所有迭代中都做同样的事情,您在 y 中递增 x

以下循环增量不正确

 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