this might not be the hardest thing to acheive, yet I'm still having problems :S:
In my little program I'm simulating a card game (http://tinyurl.com/pf9fhf4) and I need to generate a random number from the range [0,35] in increments of 5. Ergo, the possible values should be : 0, 5, 10, 15, 20, 25, 30, 35. I've tried this in a sepparate class first like this:
class RandomValue {
public static void main (String [] args){
int i =0;
do {
int n = (int) (Math.random()*36 );
if (n%5 ==0){
System.out.println(n);
i++;
}
} while (i <1);
}
}
And this works!!!
When I tried making a method that would return this generated value :
public class Tarot {
public static int rValue (){
int i =0;
do {
int n = (int) (Math.random()*36 );
if (n%5 ==0){
int r =n;
i++;
}
}while(i<1);
return r;
}
}
it returns an error:
Tarok.java:14: error: cannot find symbol
return r;
^
What am I doing wrong, any suggestions how to do this in a more pretty way?
change this code
public class Tarot {
public static int rValue (){
int i =0;
do {
int n = (int) (Math.random()*36 );
if (n%5 ==0){
int r =n;
i++;
}
}while(i<1);
return r;
}
}
to
public class Tarot {
public static int rValue (){
int i =0;
int r =0
do {
int n = (int) (Math.random()*36 );
if (n%5 ==0){
r=n;
i++;
}
}while(i<1);
return r;
}
}
reason
The scope of variable r
is within if
loop So when trying to return r compiler did not find r
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments