简体   繁体   English

如何显示void方法的内容?

[英]How Can I display the contents of void method?

I am new to programming and Java as well. 我是编程和Java的新手。 I need to write a void method which sorts the array entered, I have the code written but do not know how to display the sorted list from void method. 我需要编写一个void方法来对输入的数组进行排序,我已经编写了代码,但是不知道如何显示来自void方法的排序列表。 Anyone willing to help. 任何愿意帮助的人。 It will be greatly appreciated. 将不胜感激。

package util.assign4;

import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import util.IO;


public class UtilAssign4 {
    public static int[] getData (String input){

        StringTokenizer st = new StringTokenizer(input);
        int []x = new int[st.countTokens()];
        for(int i = 0;st.hasMoreTokens(); i++){
            try{
            x[i] = Integer.parseInt(st.nextToken());
            }
            catch(Exception e){ 
                JOptionPane.showMessageDialog(null," Invalid input");
                System.exit(1);
            }   
         }
        return x;    
    }
    public static int getHighest(int g[]){
        int  hi = g[0];
        for( int k = 1; k <g.length;k++)
            if(g[k]> hi) hi = g[k];

        return hi;
    }
    public static int getSmallest(int p[]){
        int sm = p[0];
        for(int l = 1;l<p.length;l++)
            if(p[l]  < sm) sm = p[l];
        return sm;
    }
    public static float getAverage(int n[]){
        float sum = 0.0f;
        for(int y = 0;y <n.length; y++) sum += n[y];
        return sum/n.length;

    }
    public static void getSorted(int grades []){
        for(int i = 0; i<grades.length-1;i++){
            int largest =i;
            for(int j = 0;j<grades.length;j++)
                if(grades[j]>grades[largest]) largest = j;
            int temp = grades[largest];
            grades[largest] = grades[i];
            grades[i]=temp;  

        }


    }


    public static void main(String[] args) {
       String input = JOptionPane.showInputDialog("Enetr one or more grades:");
       int [] x = getData(input);
       int j = getHighest(x);
       int m = getSmallest(x);
       float a = getAverage(x);



               IO.showMsg("Array you entered:" + input + String.format("\nThe"
                       + " higheset grade is:%2d \n"
                       + "The Lowest Grade is:%2d \n"+"The average is:%2.2f\n"
                       + "The sorted list: ",
                       j,m,a));


    }
}

You are not supposed to print the contents of the array in the sort method. 您不应该在sort方法中打印数组的内容。 Your requirement (I wager) is to sort the array supplied to the method 'in-place' (which it already looks like you are doing). 您的要求(我押注)是对提供给“就地”方法的数组进行排序(它看起来就像您在做的那样)。 What this means is that given an array: 这意味着给定一个数组:

int[] grades = new int[] {34, 76, 12, 0, -1};

That when you call: 当您致电时:

UtilAssign4.getSorted(grades);

That the array passed into the method is actually sorted inside the method, and as such does not need to be returned (that's why your return type is void). 传递给方法的数组实际上是方法内部排序的,因此不需要返回(这就是您的返回类型为void的原因)。 So to summarize, before calling the sort method, your array is unsorted. 综上所述,在调用sort方法之前,您的数组未排序。 After the call completes, tbe very same array has now been sorted. 调用完成后,现在已经对相同的数组进行了排序。

So now you can then print out the sorted array in the calling method (in this case main(String[]) : 因此,现在您可以在调用方法中打印出排序后的数组(在本例中为main(String[])

getSorted(x); // <-- call the sort function, on your array

String msg = String.format("\nThe higheset grade is:%2d \n"
        + "The Lowest Grade is:%2d \nThe average is:%2.2f\n"
        + "The sorted list: %s", j, m, a, Arrays.toString(x));
IO.showMsg(msg);

Note the Arrays.toString(x) ? 注意Arrays.toString(x)吗? That will take your sorted array, and convert it into a string representation (will look something like this: [76, 34, 12, 0, -1]). 这将采用您排序后的数组,并将其转换为字符串表示形式(如下所示:[76,34,12,0,-1])。

in void method short your array in any field Array that is 在void方法中,将您的数组缩短为

 public class UtilAssign4 {
 private Integer[] shorted = new Integer[100];
 public static int[] getData (String input){
 .
 .

}

and do your stuff with above array in your void method and use this where you want 并在您的void方法中使用上述数组进行处理,并在需要的地方使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM