mardi 31 mars 2015

How can i put my random values into an array in java?

I'm making a program that uses selection sort on a set of integers, but im making it better by allowing the user to ask how many integers they want to be sorted, if they want to input the integers or want the integers randomly generated. Ive coded the program to be able to generate the random integers so far, but i haven't gotten to the user being able to generate their own numbers.


How am i able to take those randomly generated numbers, and put them into an array so that i can use Selection sort on those values.


Thanks



package sortingexcersices;

import java.util.Scanner;
import java.util.Random;

public class SortingExcersices {

public static int myarray[], numOfElements, arrValues, n;

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("How many integers do you want to sort?");
numOfElements = in.nextInt();

myarray = new int[numOfElements];

boolean found = false;

do {

System.out.println("If you would like random numbers, press 1.");
System.out.println("If you would like to generate your own numbers, press 2.");

n = in.nextInt();

if (n == 1) {
found = true;
} else if (n == 2) {
found = false;
} else {
System.out.println("Your input was invalid.");
}

} while (n != 1 && n != 2);

if (found = true) {
Random values = new Random();

for (int i = 1; i <= myarray.length; i++) {
arrValues = 1 + values.nextInt(50);
System.out.print(arrValues + " ,");


}
}

}
}




Aucun commentaire:

Enregistrer un commentaire