Reputation: 27
import java.util.Scanner; public class Game {
public static void main (String [] args) {
Scanner console = new Scanner (System.in);
System.out.println("Playing cards program.");
System.out.println("Give me some numbers and");
System.out.println("I will tell you the appropriate playing card. ");
System.out.println("How many numbers?");
Double a = console.nextDouble();
if (a < 0) {
System.out.println("Error should be a positive");
System.out.println("Re-Enter: ");
Double b = console.nextDouble();
}
else if (a > 14 || b >14); {
System.out.println("Error out of range (1-14) ");
System.out.println("Re-Enter: ");
Double c = console.nextDouble();
}
if ((a >= 2 && a <= 10)|| (b >= 2 && a <= 10) || (c >= 2 && c <= 10)) {
if (a ==2 || b == 2 || c == 2) {
System.out.println("You have a two");
}
else if (a ==3 || b == 3 || c == 3) {
System.out.println("You have a three");
}
else if (a ==4|| b == 4 || c == 4) {
System.out.println ("You have a four");
}
else if (a ==5 || b == 5 || c == 5) {
System.out.println ("You have a five");
}
else if (a ==6 || b == 6 || c == 6) {
System.out.println ("You have a six");
}
else if (a ==7 || b == 7 || c == 7) {
System.out.println ("You have a seven");
}
else if (a ==8 || b == 8 || c == 8) {
System.out.println ("You have a eight");
}
else if (a ==9 || b == 9 || c == 9) {
System.out.println ("You have a nine");
}
else if (a ==10 || b == 10 || c == 10) {
System.out.println ("You have a ten");
}
}
if ((a ==1 || a == 11) || (b ==1 || b== 11)||(c ==1 || c == 11)) {
System.out.println ("You have a Ace");
}
if (a== 12 || b == 12 || c == 12) {
System.out.println ("You have a Jack");
}
if (a== 13 || b == 13 || c == 13){
System.out.println ("You have a Queen");
}
if (a== 14 || b == 14 || c == 14) {
System.out.println ("You have a King");
}
}
When I compile the program I get an error of cannot find symbol variable b and c. I tried changing the or sign to add but still shows the same error
Upvotes: 0
Views: 145
Reputation: 1041
b
or c
are not always declared because you put the declaration within an if statement. If the if is not satisfied the variables are not declared
Upvotes: 0
Reputation: 95558
b
and c
are defined inside if
and else if
blocks. Hence their scopes are only local to those blocks, and will not be visible outside. Move the declaration of b
and c
to be outside those blocks:
Double a = console.nextDouble();
Double b = 0;
Double c = 0;
if (a < 0) {
...
b = console.nextDouble();
} else if (a > 14 || b > 14); {
...
c = console.nextDouble();
}
Read this for more information about variable scope in Java.
Upvotes: 2