簡體   English   中英

java如何顯示void方法的內容

[英]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(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

  }
}

你在所有的迭代中都做同樣的事情,你在你的y中增加x

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM