• Log in or sign up for an account here: https://www.sythe.org/login

JAVA : DICE ROLLS 20 TIMES and PRINTS THE RESULT

paosshole

Newcomer
Joined
Dec 18, 2009
Sythe Gold
0
Messages
1
I need help, how do I make a basic dice game that uses JOption or BufferedReader, the dice rolls 20 times and prints the result, please help me..T_T
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
Do you have any code yet? This is a very simple program, but we are not just going to give it to you.
 
As a plan to make your program:
- create a method that will return a random number between 1 and 6
- in your main() method, create a loop that will call the above method 20 times and print the result

Also, what is the purpose of the JOptionPane or BufferedReader (perhaps to figure out how many rolls to do)? If it is, look into the java.lang.Integer class to find out how to parse an int from a String.
 
This is very simple, at least show that you put some effort trying to write this program and we can help you along the way. You will want to create a new Random.


Here I wrote you a random integer generator took 3 seconds.
Just loop it 20 times and you are set. What do you need with JOption or BufferedReader? Isn't the simplest answer almost always the right one?

Code:
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(6);
System.out.println(randomInt);
 
This is very simple, at least show that you put some effort trying to write this program and we can help you along the way. You will want to create a new Random.


Here I wrote you a random integer generator took 3 seconds.
Just loop it 20 times and you are set. What do you need with JOption or BufferedReader? Isn't the simplest answer almost always the right one?

Code:
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(6);
System.out.println(randomInt);
This wouldn't work, as 0 would be a possible value, and 6 wouldn't.
Something along the lines of:
Code:
    private static int rand(int min, int max) {
        return ((int)((Math.random() * (max - min + 1)) + min));
    }
Would work.

EDIT: Or something like
Code:
private static int rand() {
    int i = rand.nextInt(7);
    if(i != 0)
        return i;
    return rand();
}
In which rand is an instance of java.util.Random.
 
This wouldn't work, as 0 would be a possible value, and 6 wouldn't.
Something along the lines of:
Code:
    private static int rand(int min, int max) {
        return ((int)((Math.random() * (max - min + 1)) + min));
    }
Would work.

EDIT: Or something like
Code:
private static int rand() {
    int i = rand.nextInt(7);
    if(i != 0)
        return i;
    return rand();
}
In which rand is an instance of java.util.Random.

I am rusty on the Random method, haven't had to use it in quite a while. Thanks for the correction.

Wouldn't this just work?

Code:
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(6);
int rolledint = randomint + 1;
System.out.println(rolledint);
 
This would be your best solution :

Code:
Random randomGenerator = new Random();
int rolls[] = new int[20];

for(int i =0; i<20; i++{
int randomInt = (randomGenerator.nextInt(6))+1;
rolls[i]=randomInt;
[COLOR="YellowGreen"]// this is if you want it stored in an array[/COLOR]
System.out.println(rolls[i]);
}
----

I hope your problems are solved, you havent posted back yet..
 
This would be your best solution :

Code:
Random randomGenerator = new Random();
int rolls[] = new int[20];

for(int i =0; i<20; i++{
int randomInt = (randomGenerator.nextInt(6))+1;
rolls[i]=randomInt;
[COLOR="YellowGreen"]// this is if you want it stored in an array[/COLOR]
System.out.println(rolls[i]);
}
----

I hope your problems are solved, you havent posted back yet..

Wtf is that shit, why would you loop something 20 times for nothing to get a random integer?

Code:
	public static int displayRandomInteger(int max) {
		return (int)(java.lang.Math.random() * (max + 1));
	}

Easy as that.

Edit: Forgot about the dice thing, stupid me :P
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
^It needs to loop 20 times... >_>.
 
duelarena.gg
Back
Top