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

RandomNumGen and Addition + Multiplication  RSS feed

 
Isaac Ade
Greenhorn
Posts: 7
Eclipse IDE Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been assigned a piece of homework where I am required to print three random numbers, add them then multiply them. I have already printed the random numbers and I require help with adding and multiplying
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isaac,

Any time you want to work with the same number in multiple operations, you need to capture the number--save them in variables so you can use them.

Les
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isaac Ade wrote:I have been assigned a piece of homework where I am required to print three random numbers, add them then multiply them.

Were you told to use Math.random()? Because there's a much simpler way to get a random int. Have a look at java.util.Random (←click).

I have already printed the random numbers and I require help with adding and multiplying

Adding and multiplying how? Do you mean the sum and product of your three numbers? If so, read Les's post.

Winston
 
Isaac Ade
Greenhorn
Posts: 7
Eclipse IDE Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Isaac Ade wrote:I have been assigned a piece of homework where I am required to print three random numbers, add them then multiply them.

Were you told to use Math.random()? Because there's a much simpler way to get a random int. Have a look at java.util.Random (←click).

I have already printed the random numbers and I require help with adding and multiplying

Adding and multiplying how? Do you mean the sum and product of your three numbers? If so, read Les's post.

Winston


I was told to use Math.random() and thank you.
 
Isaac Ade
Greenhorn
Posts: 7
Eclipse IDE Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Isaac,

Any time you want to work with the same number in multiple operations, you need to capture the number--save them in variables so you can use them.

Les

Thank you Les I'm going to test this now.
 
Isaac Ade
Greenhorn
Posts: 7
Eclipse IDE Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Isaac,

Any time you want to work with the same number in multiple operations, you need to capture the number--save them in variables so you can use them.

Les

Would you be able to give me an example on how to use the --save command
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isaac Ade wrote:Would you be able to give me an example on how to use the --save command

There's no 'save' command; it's just an assignment or a modification, viz:
  int sum = 0;
  ...
  sum = sum + iRandNum;


HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55677
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to random numbers. Never mind what the Java™ Tutorials (scroll to bottom of page) says, I still think it is usually better not to us Math#ranom. One of the things about having been ere for a long time (ten years in my case) is that you can remember old discussions, like this thread about different ways to ge “random” numbers.

And welcome to the Ranch
 
Isaac Ade
Greenhorn
Posts: 7
Eclipse IDE Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Back to random numbers. Never mind what the Java™ Tutorials (scroll to bottom of page) says, I still think it is usually better not to us Math#ranom. One of the things about having been ere for a long time (ten years in my case) is that you can remember old discussions, like this thread about different ways to ge “random” numbers.

And welcome to the Ranch

Thanks Campbell
 
Campbell Ritchie
Marshal
Posts: 55677
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for delay in replying, but that's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!