They're helping us

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

It's a question of getting 10 random integers and sorting them in ascending order, but I keep getting errors, so I have a headache... Help me

import java.util.Scanner; import java.util.Arrays;

public class Asending { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int intArray[] = new int[10]; int i;

System.out.println ("Enter 10 integers:");

    for (i=0; i<intArray.length; i++) {
        intArray[i] = scan.nextInt();
        for (int j=i+1; j<intArray.length; j++) {
            if (intArray[i]>intArray[j]) {
                int tmp = intArray[j];
                intArray[j] = intArray[i];
                intArray[i] = tmp;
                       }
        }

    }

    System.out.println(Arrays.toString(intArray)+ " ");
    scan.close();
}

}

java

2022-09-20 10:50

1 Answers

intArray[i] = scan.nextInt();

for (int j=i+1; j<intArray.length; j++) I think there's a problem here.

They told me to get the intArray[1] value and compare it with the intArray[2] value right away intArray[2] is a null value of zero because we haven't received a value yet. Of course, if you compare 0 with the input value, 0 will be smaller, right?

Maybe we should separate the process of sorting out the number input process!!


2022-09-20 10:50

If you have any answers or tips


© 2022 pinfo. All rights reserved.