Java question

Status
This thread has been locked.

Pyro

ⓟⓨⓡⓞ
Supreme
Feedback score
0
Posts
1,260
Reactions
543
Resources
0
This is a quick homework question
I want to write an int statement that represents a dice that has 6 sides.
How would I write it?
int Dice1 = ?
I want to know how I would get this so it would have 6 sides in total
Thankss
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Pyro

ⓟⓨⓡⓞ
Supreme
Feedback score
0
Posts
1,260
Reactions
543
Resources
0
6? I'm not exactly sure what your asking. If your seeing sides, 6 would be fine since it has 6 sides.
I need it so that the Dice has 6 sides, and when I make something to roll it, the dice would land on either 1-6
 

JustRayz

Feedback score
4
Posts
516
Reactions
119
Resources
0
int i = 0;
Random rand = new Random();
i = rand.nextInt(5) + 1;

=====
I dont know if that's what your looking for, but what it does is sets int i to a random integer from 1-5
 
Last edited:

Pyro

ⓟⓨⓡⓞ
Supreme
Feedback score
0
Posts
1,260
Reactions
543
Resources
0
int i = 0;
Random rand = new Random();
i = rand.nextInt(5) + 1;

=====
I dont know if that's what your looking for, but what it does is sets int i to a random integer from 0-5
I do think that is what I was looking for :p
Thanks!

Set it to 6 and then in the random do nextInt(dice1)+1
Thanks, going to try and test it out! :)
 

Ammar T

Programming Teacher
Supreme
Feedback score
7
Posts
820
Reactions
394
Resources
0
int i = 0;
Random rand = new Random();
i = rand.nextInt(5) + 1;

=====
I dont know if that's what your looking for, but what it does is sets int i to a random integer from 0-5
Nice but this would also work (it's only one line):
Code:
int diceRoll = new Random().nextInt(6) + 1;
 

Ammar T

Programming Teacher
Supreme
Feedback score
7
Posts
820
Reactions
394
Resources
0
You sure? I'm no Java mastermind but wouldn't 5 be the Max value?
Nah, he wants a dice that rolls 1 - 6. To get that, he is correct by doing +1 at the end since it will increment each by 1 making 5 into 6 and 0 into 1 etc.
 

Ammar T

Programming Teacher
Supreme
Feedback score
7
Posts
820
Reactions
394
Resources
0
Yeah, I know that. Please read the OP post I quoted.
Ahh, sorry misread. The quote isn't fully posted and so I was confused :p
 

morganjp

Retired Staff Member
Supreme
Feedback score
6
Posts
494
Reactions
755
Resources
0
How did I read it wrong? You said it will do 0-5
Let's just all agree that we're bad at reading stuff properly. As I just discovered.
 

Ammar T

Programming Teacher
Supreme
Feedback score
7
Posts
820
Reactions
394
Resources
0
Status
This thread has been locked.
Top