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!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Taking away

Bod McLeon
Ranch Hand
Posts: 65
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
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
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

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
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

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.