簡體   English   中英

在數組中使用用戶輸入對數字進行排序 Java

[英]Sorting numbers with user input in an array Java

我是 java 的初學者,我有一個簡單的問題。 我可以按順序得到數字,但只有一定數量的數字被添加到數組中。 當有一個數字小於最小數字時,它會替換那個最小數字(之前的最小數字從數組中刪除)。 我該怎么做才能填充所有陣列點?

public class apples {
    public static void main(String[] args) {
        System.out.print("How many numbers: ");
        int num=IO.readInt();
        double array[]=new double[num];
        double temp;
        boolean fixed=false;

        while (fixed==false){
            fixed=true;

            for(int j=0; j<num;j++){
                double x = IO.readDouble();
                array[j] = x;

                for (int i = 0; i < ( num - 1 ); i++) {
                    for (j = 0; j < num - i - 1; j++) {
                        if (array[j] > array[j+1]) {
                            temp = array[j];
                            array[j] = array[j+1];
                            array[j+1] = temp;
                            fixed=false;
                         }
                    }
                }

                System.out.println("Sorted list of integers:");
                for (int i = 0; i < num; i++) 
                    System.out.println(array[i]);
            }
        }
    }
}

首先讀取數字,然后對數組進行排序(它是double而不是int的數組)。 就像是,

public static void main(String[] args) {
    System.out.print("How many numbers: ");
    int num = IO.readInt();
    double array[] = new double[num];
    for (int j = 0; j < num; j++) {
        System.out.printf("Please enter double %d:%n", j + 1);
        array[j] = IO.readDouble();
    }
    System.out.println("unsorted array: " + Arrays.toString(array));
    Arrays.sort(array); // <-- or however you want to sort the array.
    System.out.println("sorted array: " + Arrays.toString(array));
}
public static void main(String[] args) {
        System.out.print("How many numbers: ");
        int num = IO.readInt();
        double array[] = new double[num];
        for (int i = 0; i < num; i++) {
            System.out.print("["+i+"]Please enter your double");
            array[i] = IO.readDouble();
        }

        //Sort
        double temp = 0;
        for (int i = 0; i < num - 1; i++) {
            for (int j = i + 1; j < num ; j++) {
                if (array[j] > array[j]) {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                 }
            }
        }

        //Result
        for(int i = 0; i < array.length; i++){
            System.out.print(array[i] + " ");
        }
    }
import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
    Scanner sc =new Scanner(System.in);
    int n =sc.nextInt();
    int arr[]=new int [n];

    for (int i =0;i<n;i++){
        arr[i]=sc.nextInt();}

        Arrays.sort(arr);
        //if(n%2==0){median}
        
        System.out.println( Arrays.toString(arr));

    }
}

暫無
暫無

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

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