简体   繁体   中英

Sorting numbers with user input in an array Java

I'm a beginner at java and I have a hopefully simple question. I can get the numbers in order, but only a certain amount of numbers are added to the array. When there is a number less than the smallest number, it replaces that smallest number (the previous smallest gets removed from array). What can I do so that all of the array spots can be filled in?

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]);
            }
        }
    }
}

First read in the numbers, then sort the array (and it's an array of double not int ). Something like,

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));

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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