Hello everyone. Thank you in advance for your time. I want this program to generate user defined amount of random number between 0-999 and then sort them in ascending order. It works properly without the bubble sort, but when i add the bubble sort it gives me a ridiculous output. like below
Output
Please Enter the amount of random numbers you want to generate : 2
229
Sorted array
229
891
Sorted array
891
///////////////////
Output I need
Please Enter the amount of random numbers you want to generate : 3
229
45
251
Sorted array
45
229
251
My code so far
package random; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please Enter the amount of random numbers you want to generate :"); int input = in.nextInt(); Random dice = new Random(); int number; for (int counter = 1; counter <= input; counter++) { number = 1 + dice.nextInt(999);// Randomly generated values between 0-999 { int nums[] = {number}; // Passing randomly generated values to array int size = nums.length; for (int i = 0; i < size; i++) { System.out.println(nums[i]);// Display original array } // Sorting for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { if (nums[i] < nums[j]) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } } //Display sorted array System.out.println("Sorted array"); for (int i = 0; i < size; i++) { System.out.println("" + nums[i]); } System.out.println(); } } } }