我輸入飲料時出現問題,例如水,茶,蘇打水。Java Vender Loop初學者
此外,我不希望它打印返回,直到用戶輸入飲料選項後,但每次都一直顯示。
import java.util.Scanner;
public class Vending
{
public static void main(String[] args)
{
System.out.print("Enter a command = ");
double balance = 0.0;
String a;
// scanner input
Scanner in = new Scanner(System.in);
while (in.hasNext())
{
String command = in.nextLine();
if (command.equals("penny"))
{
balance = balance + 0.01;
System.out.println("balance = " + balance);
}
if (command.equals("nickel"))
{
balance = balance + 0.05;
System.out.println("balance = " + balance);
}
if (command.equals("dime"))
{
balance = balance + 0.10;
System.out.println("balance = " + balance);
}
if (command.equals("quarter"))
{
balance = balance + 0.25;
System.out.println("balance = " + balance);
}
if (command.equals("dollar"))
{
balance = balance + 1.00;
System.out.println("balance = " + balance);
}
else if (command.equals("break"))
{
break;
}
else if (command.equals("water"))
{
System.out.println("Water");
System.out.println("balance = " + balance - 1.25);
}
else if (command.equals("tea"))
{
System.out.println("Tea");
System.out.println("balance = " + balance - 1.50);
}
else if (command.equals("soda"))
{
System.out.println("soda");
System.out.println("balance = " + balance - 1.75);
}
else
{
System.out.println("return " + balance + " to customer");
}
balance++;
}
}
}
是的,我會,如果你使用的是Java 7,但仍然有運算代碼其他錯誤也推薦這個。 – Breavyn 2013-03-18 07:53:58