[英]ERROR CANT FIND SYMBOL (FOR LOOP)
每當我運行此程序時,它說它找不到SHOTS的符號(最后一行),所以我認為問題是因為我在FOR LOOP中聲明了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 $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" ) ;
}
}
}
}
然后我嘗試進行一些更改,方法是在FOR LOOP之外聲明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 $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;
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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.