Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

print out two random numbers  RSS feed

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I am stuck on this homework assignment and really need help. I am suppose to print out two random numbers and the bigger number is suppose to subtract the smaller number. Yet when the smaller number subtracts the bigger number it also outputs the smaller number subtracting the bigger number. Not only that but I need a response for the user to solve the problem and if the answer is correct I then want it to display saying correct. If not I want it to display incorrect with what the problem was and the actual answer. The following is what I have so far. Thanks!!!

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In future, while posting code, please UseCodeTags
I have added them this time for you. As you can see the tags make the code much more readable and easy to understand.

Junaid Mahmud wrote:Yet when the smaller number subtracts the bigger number it also outputs the smaller number subtracting the bigger number.

Consider this line of code

Do you see what is wrong here?

Junaid Mahmud wrote:Not only that but I need a response for the user to solve the problem and if the answer is correct I then want it to display saying correct. If not I want it to display incorrect with what the problem was and the actual answer.

So you have
1) Generated two random numbers (By the way check out Random#nextInt(int bound);)
2) Posed the question to the user

TODO
3) Figure out what the user answered
4) Evaluate the answer
5.a) If correct, display "correct"
5.b) If not, display "incorrect" and explanation

So do you know how to go about step 3? If not check out https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Maneesh is right; avoid the Math#random method because the arithmetic can be complicated and error‑prone. What do you think the results of your two “random” calculations will be?
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still very confused on step 3
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junaid Mahmud wrote:I am still very confused on step 3


From the description, step 3 is merely using the Scanner instance to read what the user entered. What have you tried? And what issues are you encountering?

Henry
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junaid Mahmud wrote:I am still very confused on step 3

Did you check the link to the Scanner class I had posted? It contains explanations and sample code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!