错误无法找到符号(FOR 循环)

ERROR CANT FIND SYMBOL (FOR LOOP)

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

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 .00");
            System.out.println("There are 100 coffee shots in stock and each costs .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" ) ;
            }
        }
    }
}

然后我尝试做一些改变,通过在 FOR 循环之外声明 SHOTS,我想到了这个,通过让我的输出(SHOTS)是我输入的最后一个数字:例如当我他们问有多少镜头,我说 2,3,4,.. 所有杯子(1,2 和 3)都有 4 次拍摄,我想为 cup1 拍摄 2 次,为 2 号杯拍摄 3 次,为 3 号杯拍摄 4 次。这就是一个例子

 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 .00");
  System.out.println("There are 100 coffee shots in stock and each costs .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;
   int shots=0;
     for (countd = 1; countd<= CupsOfCoffee; countd++)
   {
      System.out.println("How many coffee shots in cup "+ countd);

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

   }
   }
   }
   }

我想我应该将 SHOTS 值存储在一个 int[] 数组中。

我可以看到几个问题。首先,在最后一行的 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" ) ;

请注意,数组索引从零开始,因此我更新了 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");
}