Java 使用int变量控制Do…While()循环是';行不通

Java 使用int变量控制Do…While()循环是';行不通,java,java.util.scanner,Java,Java.util.scanner,编辑:问题是我没有在它之外声明变量 我的循环无法将int解析为变量 无法尝试其他类型的变量,它必须是整数 do{ System.out.println(“您想添加零食吗?”); System.out.println(“1:($5)大爆米花”); System.out.println(“2:($0)无其他”); int snackChoice=input.nextInt(); System.out.println(“选项”+snackChoice+“您想要多少?”); System.out.pri

编辑:问题是我没有在它之外声明变量

我的循环无法将int解析为变量

无法尝试其他类型的变量,它必须是整数

do{
System.out.println(“您想添加零食吗?”);
System.out.println(“1:($5)大爆米花”);
System.out.println(“2:($0)无其他”);
int snackChoice=input.nextInt();
System.out.println(“选项”+snackChoice+“您想要多少?”);
System.out.println(“1:1”);
System.out.println(“2:2”);
System.out.println(“3:3”);
System.out.println(“4:4”);
系统输出打印号(“5:5”);
int snackAmount=input.nextInt();
开关(snackChoice){
案例1:
成本=成本+5*小吃金额;
System.out.println(“您的税前总额为:$”+成本+(5*snackAmount));
打破
违约:
System.out.println(“不是有效选项”);
打破
}
}
while(snackChoice!=9);
其余部分:

它起作用了。实际结果:事实并非如此


错误:
snackChoice无法解析为变量

do…而
循环是一个代码块,这意味着无法在块外看到其中定义的变量。
while
部分位于块外,因此它无法看到
snackChoice

只需在
do…while
之前定义
int snackChoice
,而不在其中

int snackChoice;
do {
    System.out.println("Would you like to add any snacks? ");
    System.out.println("1: ($5) Large Popcorn");
    System.out.println("2: ($0) Nothing Else");
    snackChoice = input.nextInt();

    System.out.println("How many of option " + snackChoice + " would you like? ");
    System.out.println("1: One");
    System.out.println("2: Two");
    System.out.println("3: Three");
    System.out.println("4: Four");
    System.out.println("5: Five");
    int snackAmount = input.nextInt();

    switch (snackChoice) { 
    case 1: 
        cost = cost + 5 * snackAmount;
        System.out.println("Your total before tax will be: $" + cost + (5 * snackAmount)); 
    break;

    default: 
        System.out.println("Not a valid option."); 
    break;

    }
}
while(snackChoice != 9);

do…while
循环是一个代码块,这意味着在其中定义的变量无法在块外看到。
while
部分位于块外,因此它无法看到
snackChoice

只需在
do…while
之前定义
int snackChoice
,而不在其中

int snackChoice;
do {
    System.out.println("Would you like to add any snacks? ");
    System.out.println("1: ($5) Large Popcorn");
    System.out.println("2: ($0) Nothing Else");
    snackChoice = input.nextInt();

    System.out.println("How many of option " + snackChoice + " would you like? ");
    System.out.println("1: One");
    System.out.println("2: Two");
    System.out.println("3: Three");
    System.out.println("4: Four");
    System.out.println("5: Five");
    int snackAmount = input.nextInt();

    switch (snackChoice) { 
    case 1: 
        cost = cost + 5 * snackAmount;
        System.out.println("Your total before tax will be: $" + cost + (5 * snackAmount)); 
    break;

    default: 
        System.out.println("Not a valid option."); 
    break;

    }
}
while(snackChoice != 9);

我有snackChoice 9退出循环,还有其他方法可以选择退出循环吗?我需要用户能够选择任意数量的零食。这很好。只需在
do{
语句之前定义变量本身,这样它就可以存在于
do..while
block@BellaStallman在这里,我已经将修改后的代码添加到我的答案中。好的,谢谢。当运行时,我现在尝试选项9,它会询问我想要多少。无论我选择哪个选项(1,5,12)它给了我无效的选项并再次询问。我现在如何才能正确退出?我有snackChoice 9退出循环,是否有其他方法可以通过选择退出循环?我需要用户能够选择任意数量的零食和数量。这很好。只需在
之前定义变量本身即可{
语句-因此它将存在于
do..while
block@BellaStallman在这里,我将修改后的代码添加到我的答案中。好的,谢谢。当运行时,我现在尝试选项9,它会询问我想要多少。无论我选择哪个选项(1,5,12),它都会给我无效的选项并再次询问。我现在如何才能正确退出此选项?