01-03-2013, 11:45 AM
Awesome work!
You could improve this program by using an array to keep track of how many of each number there are, however. Like so:
[code2=java]import java.util.Random;
public class RandomNumForum {
private static final int RANDOM_COUNT = 100;
public static void main(String[] args){
int intCount[] = new int[7];
int i = 0;
/*To change the amount of random numbers generated
* change the RANDOM_COUNT var
* to your amount of numbers
*/
while (i < RANDOM_COUNT){
Random rnd = new Random();
int randomInteger = rnd.nextInt(7);
System.out.println(randomInteger);
intCount[randomInteger]++;
i++;
}
System.out.println("There were " + intCount[1] + " ones");
System.out.println("There were " + intCount[2] + " twos");
System.out.println("There were " + intCount[3] + " threes");
System.out.println("There were " + intCount[4] + " fours");
System.out.println("There were " + intCount[5] + " fives");
System.out.println("There were " + intCount[6] + " sixes");
System.out.println("There were " + intCount[0] + " zeros");
}
}[/code2]
You could improve this program by using an array to keep track of how many of each number there are, however. Like so:
[code2=java]import java.util.Random;
public class RandomNumForum {
private static final int RANDOM_COUNT = 100;
public static void main(String[] args){
int intCount[] = new int[7];
int i = 0;
/*To change the amount of random numbers generated
* change the RANDOM_COUNT var
* to your amount of numbers
*/
while (i < RANDOM_COUNT){
Random rnd = new Random();
int randomInteger = rnd.nextInt(7);
System.out.println(randomInteger);
intCount[randomInteger]++;
i++;
}
System.out.println("There were " + intCount[1] + " ones");
System.out.println("There were " + intCount[2] + " twos");
System.out.println("There were " + intCount[3] + " threes");
System.out.println("There were " + intCount[4] + " fours");
System.out.println("There were " + intCount[5] + " fives");
System.out.println("There were " + intCount[6] + " sixes");
System.out.println("There were " + intCount[0] + " zeros");
}
}[/code2]