gpt4 book ai didi

Java错误变量

转载 作者:行者123 更新时间:2023-12-02 04:51:13 25 4
gpt4 key购买 nike

导入java.util.Scanner;公开课游戏{

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");
}

}

当我编译程序时,出现找不到符号变量 b 和 c 的错误。我尝试更改或符号来添加,但仍然显示相同的错误

最佳答案

bcifelse if block 内定义。因此,它们的范围仅在这些 block 的本地,并且在外部不可见。将 bc 的声明移至这些 block 之外:

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有关 Java 中变量作用域的更多信息。

关于Java错误变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29218918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com