如何显示 void 方法的内容 java

huangapple go评论73阅读模式
英文:

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(&quot;Number of Students : &quot;);
int num = in .nextInt();
int[] StudNum = new int[num];
String[] StudName = new String[num];
int[] StudGrade = new int[num];
for (int x = 0; x &lt; num; x++) {
System.out.print(&quot;Student Number : &quot;);
StudNum[x] = in .nextInt(); in .nextLine();
System.out.print(&quot;Student Name   : &quot;);
StudName[x] = in .nextLine();
System.out.print(&quot;Student Grade  : &quot;);
StudGrade[x] = in .nextInt(); in .nextLine();
System.out.println();
}
String str;
do {
System.out.print(&quot;\nSort By ? [S]Student Number [G]Grade [V]View [E]Exit : &quot;);
str = in .nextLine();
if (str.matches(&quot;[vV]&quot;)) // i try removing this but nothing happens 
{
SortByView(StudNum, StudName, StudGrade);
} else if (str.matches(&quot;[sS]&quot;)) {
SortByStudNumber(StudNum, StudName, StudGrade);
} else if (str.matches(&quot;[Gg]&quot;)) {
SortByGrade(StudNum, StudName, StudGrade);
} else if (str.matches(&quot;[Ee]&quot;)) {
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 &lt; StudNum.length; x++) {
System.out.print(&quot;\n&quot; + StudNum[x] + &quot; &quot; + StudName[x] + &quot; &quot; + 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 &lt; StudNum.length; x++) {
for (int y = 0; y &lt; StudNum.length - 1; x++) {
if (StudNum[y] &gt; 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 &lt; StudNum.length; x++) {
System.out.print(&quot;\n&quot; + StudNum[x] + &quot; &quot; + StudName[x] + &quot; &quot; + StudGrade[x]); // pls display this output 
}
}
public static void SortByGrade(int[] StudNum, String[] StudName, int[] StudGrade) {
// sort by grade
for (int x = 0; x &lt; StudNum.length; x++) {
for (int y = 0; y &lt; StudNum.length - 1; x++) {
if (StudGrade[y] &gt; 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 &lt; StudNum.length; x++) {
System.out.print(&quot;\n&quot; + StudNum[x] + &quot; &quot; + StudName[x] + &quot; &quot; + StudGrade[x]); // pls display this output 
}
}
public static boolean isExit(String input) {
return input.matches(&quot;[eE]&quot;) &amp;&amp; input.length() == 1;
}
public static boolean TestInput(String input) {
return input.matches(&quot;[sSvVgGeE]&quot;) &amp;&amp; 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 &lt; StudNum.length -1; x++)

After fixing it to

for(int y = 0 ; y &lt; 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 &lt; StudNum.length; x++) {
for (int y = 0; y &lt; StudNum.length - 1; x++) { // &lt; - 这里你增加了 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 &lt; StudNum.length; x++) {
for (int y = 0; y &lt; StudNum.length - 1; x++) { // &lt;- 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.

huangapple
  • 本文由 发表于 2020年9月18日 21:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63956461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定