• Post Reply Bookmark Topic Watch Topic
  • New Topic

random dice generator  RSS feed

 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm new to the forum and a total java beginner
I'm trying to make a program that rolls two dices and tells me if I got a pair or if I got two sixes. If I get two sixes it should print "You lucky bastard".
It will only print "You got a pair" and "You didn't get a pair". Have no idea what I'm missing.
And also I dont understand why I cant just type int num1, num2; Why do I have to declare both num1 and num2 as 0 in order for it to run?

What I'd like the program to do as well is to print out the rolls together with either of the statements.

It would be of tremondous help if someone could correct me and show me the best way to make a program like that.





 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Do you really need the for loops? If not, remove them. If so, what are they supposed to do? Post your modified code and we'll go from there.
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Welcome to the Ranch!

Do you really need the for loops? If not, remove them. If so, what are they supposed to do? Post your modified code and we'll go from there.


Thank you

I started yesterday and I'm learning from some youtube tutorials. I don't know any other way or I've simply forgot how to. I find it very hard to think and come up with better ways to program since everything is so new to me.

But I just figured out that since I only roll each dice once I dont need the for loop, right?

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. Then you can combine the declarations (int num1) with the setting of the value.

Oracle has some good tutorials that you can go through if you want.
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modified it like you said and now I see that I don't have to use num1=0 num2=0. But still it wont tell me when I get two sixes..

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's look at the if statements:

(Notice I added indentation to show the logic. Always do this.) Go through this line by line. Pretend you have to sixes. What line will execute?
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Let's look at the if statements:

(Notice I added indentation to show the logic. Always do this.) Go through this line by line. Pretend you have to sixes. What line will execute?




Yeah its logical that just the first line executes since two sixes is a pair. But why cant it listen to my second line so when two sixes come it prints "You lucky bastard" instead of "I got a pair" How can I make the program to do this ?

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why cant it listen to my second line so when two sixes come it prints "You lucky bastard" instead of "I got a pair" How can I make the program to do this ?

It's a matter of logic. In general, put the most restrictive test as the first test:
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it now, i think :P

 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
But why cant it listen to my second line so when two sixes come it prints "You lucky bastard" instead of "I got a pair" How can I make the program to do this ?

It's a matter of logic. In general, put the most restrictive test as the first test:



Thank you! Everything is clear now
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. Happy programming!
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I forgot. Is there any way so that I can print the numbers with each statement. So it prints e.g.
You got a pair 3 3
You lucky bastard 6 6
You didn't get a pair 5 3

EDIT: I got it now. Made another stupid mistake so even though I did right it wouldnt show up.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As well as indenting ifs, fors, etc, always give them pairs of braces {}. It makes it much easier to see where the block is, and to add additional statements inside the block. Also remember there is no semicolon after the ). That is a mistake everybody makes, so you will one day write
… and not understand why it prints Success when i is -1234567890.

There is a conventional form for a for loop, and there is a good reason for that. It is
for (int i = 0; i < j; i++) {...} // Value of j is known.
Note the 0 and the >. If you are using an array don't write a number for j, write myArray.length. Always start from that conventional form. The reason is that such a loop will always work, will always run j times, and if you have an array it will never throw an Exception. Sometimes you will need the loop to run j + 1 times or j − 1 times in which case you can change the numbers in the header.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:

Sorry, but this code is wretched, simply because the formatting is so bad. you should get in the habit NOW of writing code correctly, else you will be in a world of pain later on. the above should be formatted more like this:


1) Sure, you can debate the exact curlie brace placement, but they should be there for every if/else/while block. See Campbell's post above.
2) using "l" as a variable name is a bad idea. It is EXTREMELY hard to tell if it is a "l", a "I", a "|" or even a "1" depending on your font. If you use it, someday you will spend a ridiculous amount of time tracking down why something doesn't work.
3) The indentation you DID have, you sometimes uses spaces, and sometimes used tabs. that really messes up the formatting when someone else loads your code if their tab-stop is a different size. Use tabs only or spaces only - never both.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:using "l" as a variable name is a bad idea...

@Cyran: As are 'num1', 'num2', 'a' and 'b'. in fact, the only name you have that actually describes something is 'dice', and it's on a singular object ("dice" is plural).

Naming is incredibly important, because it not only helps you to read your own code, but other people to understand what you're doing - and don't forget that your tutor (or anyone else you ask for help) may also to have to try to work out what you were attempting to do, so any help you can give them will be invaluable.

You also need to think before you code - or change your code. Knute's advice was general, and highlighted an error for you; but the result is still less than optimal.includes a redundant check, but:does not.

You're also allowed to write more than one method, and doing so can often help people understand what you're doing better.
For example: What is
  1+dice.nextInt(6)
?
You and I both know that it's the result of "rolling" a die (singular), but it may not be immediately obvious to someone looking at your code.
On the other hand:makes it much more obvious don't you think?

Before you go any further, I'd suggest looking at your code and seeing if you can't make it a bit more readable with a few name changes and spacing things out a bit.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with redundancy?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with redundancy?
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
fred rosenberger wrote:using "l" as a variable name is a bad idea...

@Cyran: As are 'num1', 'num2', 'a' and 'b'.
. . .
Before you go any further, I'd suggest looking at your code and seeing if you can't make it a bit more readable with a few name changes and spacing things out a bit.

HIH

Winston



Thank you and to all the other great replies I've got. I've learned a lot from them! Didn't imagine that I would get this much help, so I really appreciate it
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

By the way: don't quote the whole of a post; all that does is make the thread unnecessarily long. I have deleted most of the quoted text because it is in the previous post.
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:


Before you go any further, I'd suggest looking at your code and seeing if you can't make it a bit more readable with a few name changes and spacing things out a bit.

HIH

Winston


I corrected the code and made the changes you suggested but now it only prints out the else statement and nothing else. Also it doesnt roll new numbers but the same two numbers are used for all the rolls.

 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will produce pairs; I have tried it.
java sexortvå
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
You got a pair 22
I even got 66 once.
What you are doing it rolling the dice once, then looping 51× (not 50 as you think) without rolling them again.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:I corrected the code and made the changes you suggested but now it only prints out the else statement and nothing else. Also it doesnt roll new numbers but the same two numbers are used for all the rolls.

My apologies. I think I copied the code from your 2nd post (which was wrong).

Obviously, those assignments should go inside your loop.

Winston
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indentation: the roll method should be one level to the right.
Line 18 follows a { so everything from line 18 to line 28 should be one level to the right, too. The } at the end of line 28 should be in a line by itself.
Try installing the Eclipse IDE and opening that code in Eclipse and use ctrl‑A then ctrl‑F or ctrl‑I to get the formatting corrected. Then stop using Eclipse for at least six months because IDEs can add their own confusion.
I gave some instructions to somebody else about indenting a week and a half ago. It may seem very fussy to go on about indenting, but if you had indented the code correctly you would have seen the problem for yourself.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You haven't followed this Winston's suggestion:
2. One more thing - class names suppose to start with an upper case.
3. Your current code is incorrectly formatted. I have in mind "for", "if".
4. Notice "for" loop should be i < 50 not i <= 50, Campbell mentioned that already to you.
5. Also, make some spaces around operators. Your "main" method formatted incorrectly too. After "main" shouldn't be space before parentheses, as well as in between "String []"
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Cyran Meriel wrote:I corrected the code and made the changes you suggested but now it only prints out the else statement and nothing else. Also it doesnt roll new numbers but the same two numbers are used for all the rolls.

My apologies. I think I copied the code from your 2nd post (which was wrong).

Obviously, those assignments should go inside your loop.

Winston


Relieved it was that easy to fix Thanks!
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:1. You haven't followed this Winston's suggestion:
2. One more thing - class names suppose to start with an upper case.
3. Your current code is incorrectly formatted. I have in mind "for", "if".
4. Notice "for" loop should be i < 50 not i <= 50, Campbell mentioned that already to you.
5. Also, make some spaces around operators. Your "main" method formatted incorrectly too. After "main" shouldn't be space before parentheses, as well as in between "String []"


Great tips, thanks!

Campbell Ritchie wrote:

There is a conventional form for a for loop, and there is a good reason for that. It is
for (int i = 0; i < j; i++) {...} // Value of j is known.
Note the 0 and the >. If you are using an array don't write a number for j, write myArray.length. Always start from that conventional form. The reason is that such a loop will always work, will always run j times, and if you have an array it will never throw an Exception. Sometimes you will need the loop to run j + 1 times or j − 1 times in which case you can change the numbers in the header.


Missed the importance of this post. Now that I've learned a bit on arrays I understand what you are saying
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:
Liutauras Vilda wrote:1. You haven't followed this Winston's suggestion:..
Great tips, thanks!

It's possibly worth mentioning that this can be shortened to:
The <condition> ? x : y construct is called a 'ternary' operator (although I don't think that's its "official" name), and its quite useful forsituations - although not everybody likes it.

If you do, use it ... in moderation of course.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!