每当我运行这个程序时,它说它找不到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次射门,我想得到2次射门,1次射门2次射门,4次射门3次射门,这就是示例
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 []数组中。
答案 0 :(得分:0)
我可以看到几个问题。首先,在最后一行的println语句中,您指的是shot
而不是shots
。
其次,在shots
循环之外声明for
很好,但是你在循环中重新定义它,隐藏另一个变量。
要使用数组,请尝试以下操作:
...
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
。
答案 1 :(得分:0)
你需要使用一个数组,以便每个杯子有一个射击次数。
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");
}