-6

メイン メソッドで 5 つの整数値の配列を読み取ります。配列をソートし、配列を main メソッドに戻してそこに出力する別の関数を作成します。

これまでのところ、私はやっています:

    import java.util.*;
    public class arraysort {
        public static void main(String[]args) {
           Scanner in = new Scanner (System.in);
           System.out.println("Enter 5 integers: ");
           int [] x = new int [5];
           for (int i=0; i<5; i++) {
               x[i] = in.nextInt();
           }
        }
        public static int sortarray(int [] value) {

            int max = value[0];
            for (int i = 1; i < value.length; i++) {
//I am not sure after this point 
//I just did the rest
                int [] y = new int [5];
                Scanner in = new Scanner (System.in);
                value[i] = in.nextInt();
                Arrays.sort(y);
                System.out.println(y);
            }

        }
    }
4

2 に答える 2

0

Arrays.toString()およびが後援するコードArrays.sort():

public class ArraySort 
{
    public static void main(String args[])
    {
        int t[] = {3, 5, 2}; // your reading part is fine, so I will skip it
        sortArrayWithoutCheating(t);
        System.out.println(Arrays.toString(t));
    }

    public static void sortArrayWithoutCheating(int[] t)
    {
        // simplest O(n^2) sort
        for (int i = 0; i < t.length; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (t[i] < t[j])
                {
                    // swap t[i] and t[j]
                    int temp = t[i];
                    t[i] = t[j];
                    t[j] = temp;
                }
            }
        }
    }

    public static void sortArray(int[] t)
    {
        Arrays.sort(t);
    }
}

出力:

[2, 3, 5]
于 2013-09-05T14:07:22.550 に答える