This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Taking away  RSS feed

 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a way to generate random numbers:

or


But is there a way to take the two random numbers the generator generates, away from each other?
For example:
One generates 235 and the other 453
Is there a way to take 235 from 453?
I can't just tell it to do 1-1 all the time because they will not be the same number.
I tried taking away strings like this:


j = health
g = attack damage
But it did not work.
So is there actually away to do it?
 
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
I am not sure I understand what problem you are trying to solve exactly.
Why can't you use the Random to generate two numbers and then subtract them?

random#nextInt(int bound) - random#nextInt(int bound);
 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:I am not sure I understand what problem you are trying to solve exactly.
Why can't you use the Random to generate two numbers and then subtract them?

random#nextInt(int bound) - random#nextInt(int bound);

I see what you did but i have to generate the numbers in two separate parts of the code before i take it away.
So what i mean:
A Pokemon is assigned a random value.
It say 'Pokemon' has (Then the random value)
...
...
Going through code
...
...
later a JOptionPane appears asking if you want to use attack '1' or '2'.
(at the moment attack 2 is disabled)
So when you select attack one, it generates another random number .
This one is going to be the amount of damage the attack does.
It will then subtract it.
Take a look at my code:

                                       ^^^^^
Here is where it generates the number for the amount of health.

Hope this makes sense  
 
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

Those two variables are local variables. Local variables are only in scope during the method call.  In this case, those two local variables are from two different methods, from two different classes. There is no way to get those variables in scope at the same time.

You need to store those values in a location where they are both in scope at the same time -- so that you can do the subtraction.

Henry
 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Those two variables are local variables. Local variables are only in scope during the method call.  In this case, those two local variables are from two different methods, from two different classes. There is no way to get those variables in scope at the same time.

You need to store those values in a location where they are both in scope at the same time -- so that you can do the subtraction.

Henry

Ahh I see now. Thanks for the advice!
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In your print statement you are printing out 'h', I think you meant to print out 'Result'.

Note that the Java convention is for all variable and method names to start with a lower case letter followed by camel case.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another recommendation for good coding, use variable names that describe what the variable contains.   For example random vs h.  That will help you see mistakes when coding.
h has no meaning.  random says the value has to do with random numbers.
 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the advice!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!