programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# print out two random numbers

Ranch Hand
Posts: 118
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!!!

Bartender
Posts: 11445
18
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
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

Marshal
Posts: 57437
175
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
I am still very confused on step 3

author
Sheriff
Posts: 23364
127

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

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.

 Get out of my mind! Look! A tiny ad! The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS