Java 错误找不到符号(用于循环)

Java 错误找不到符号(用于循环),java,jvm,initialization,void,declare,Java,Jvm,Initialization,Void,Declare,每当我运行这个程序时,它都会说找不到快照的符号(最后一行),所以我认为问题是因为我在for循环中声明了快照,但是当我想打印出来时,我无法 import java.util.Scanner; public class CoffeeBot { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println ("Hell

每当我运行这个程序时,它都会说找不到快照的符号(最后一行),所以我认为问题是因为我在for循环中声明了快照,但是当我想打印出来时,我无法

import java.util.Scanner;

public class CoffeeBot {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println ("Hello, what's your name?");
        String name = keyboard.nextLine();
        System.out.println("Would you like to order some coffee, " + name + "? (y/n)");
        String goon=keyboard.next();
        char answer=goon.charAt(0);
        if ((answer!= 'y') && (answer!='n'))
            System.out.println("no valid");
        else if (answer== 'n')
            System.out.println("OK BYE");
        else {
            System.out.println("Great, Let's get started.");
            System.out.println("Order selection");
            System.out.println("----------------");
            System.out.println("There are 90 coffee cups in stock and each costs $2.00");
            System.out.println("There are 100 coffee shots in stock and each costs $1.00");
            System.out.println("How many cups of coffee would you like?");
            int CupsOfCoffee = keyboard.nextInt();
            if (CupsOfCoffee ==0)
                System.out.println("No cups, no coffee, Goodbye");
            else if (CupsOfCoffee < 0)
                System.out.println("Doesn't compute, system terminating");
            else if (CupsOfCoffee >90)
                 System.out.println("Not enogh stock,come back later");
            else {
                int countd;
                for (countd = 1; countd<= CupsOfCoffee; countd++) {
                    System.out.println("How many coffee shots in cup "+ countd);
                     int shots = keyboard.nextInt();
                }
                System.out.println("Order Suammery\n----------------");
                for (countd = 1; countd<= CupsOfCoffee; countd++)
                    System.out.println("cup " + countd + "has" + shots+  "and will cost" ) ;
            }
        }
    }
}
import java.util.Scanner;
公共级咖啡机器人{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.println(“你好,你叫什么名字?”);
字符串名称=keyboard.nextLine();
System.out.println(“您想点咖啡吗,“+name+”?(是/否)”);
String goon=keyboard.next();
char answer=goon.charAt(0);
如果((回答!='y')&&(回答!='n'))
System.out.println(“无效”);
否则如果(答案='n')
System.out.println(“好的,再见”);
否则{
System.out.println(“太好了,让我们开始吧。”);
System.out.println(“订单选择”);
系统输出打印项次(“------------------------------”);
System.out.println(“有90个咖啡杯库存,每个售价2美元”);
System.out.println(“库存中有100杯咖啡,每杯售价1美元”);
System.out.println(“您想要多少杯咖啡?”);
int CupsOfCoffee=keyboard.nextInt();
如果(杯咖啡==0)
System.out.println(“没有杯子,没有咖啡,再见”);
否则如果(杯咖啡<0)
System.out.println(“不计算,系统终止”);
否则,如果(杯咖啡>90)
System.out.println(“库存不足,稍后再来”);
否则{
int countd;

对于(countd=1;countd我可以看到几个问题。首先,在最后一行的println语句中,您指的是
shot
,而不是
shots

其次,在
for
循环的
外部声明
shots
是可以的,但随后在循环内部重新定义它,隐藏另一个变量

要使用数组,请尝试以下操作:

...
int[] shots = new int[CupsOfCoffee];
for (countd = 0; countd < CupsOfCoffee; countd++)
{
    System.out.println("How many coffee shots in cup " + (countd + 1));

    shots[countd] = keyboard.nextInt();
}
System.out.println("Order Suammery\n----------------");
for (countd = 0; countd < CupsOfCoffee; countd++)

System.out.println("cup " + (countd + 1) + " has " + shots[countd] +  " shots and will cost" ) ;
。。。
int[]shots=新int[CupsOfCoffee];
对于(countd=0;countd
请注意,数组索引从零开始,因此我更新了
countd
,以反映这一点


作为一般样式点,变量通常应以小写字母开头,因此理想情况下应使用
cupsOfCoffee

您需要使用一个数组,以便为每个杯子提供一个放炮计数

int countd;
int[] shots = new int[CupsOfCoffee];
for (countd = 1; countd <= CupsOfCoffee; ++countd) {
    System.out.println("How many coffee shots in cup "+ countd);
    shots[countd - 1] = keyboard.nextInt();
}

for (countd = 1; countd <= CupsOfCoffee; ++countd) {
    System.out.println("cup " + countd + "has" + shots[countd - 1]+  "and will cost");
}
int countd;
int[]shots=新int[CupsOfCoffee];

对于(countd=1;countd您的shift键有什么问题吗?;-)您可以用反勾(
`
)将关键字括起来,漂亮地打印关键字别再喊了,人们都想睡觉了。是的,这是一个打字错误,镜头也不能运行,我会在这里编辑这个,谢谢。我试着在循环外声明它作为我的第二个例子,但我没有得到我想要的,我只得到我为所有杯子输入的最后一个值,正如我在我的博文《谢谢兄弟》中解释的!我成功了放弃我的生命,让我们完成这段代码:再次感谢你!干杯:)坦克人
int countd;
int[] shots = new int[CupsOfCoffee];
for (countd = 1; countd <= CupsOfCoffee; ++countd) {
    System.out.println("How many coffee shots in cup "+ countd);
    shots[countd - 1] = keyboard.nextInt();
}

for (countd = 1; countd <= CupsOfCoffee; ++countd) {
    System.out.println("cup " + countd + "has" + shots[countd - 1]+  "and will cost");
}