0
需要Java中if語句的幫助。當項目爲0時,需要程序說「對不起,缺貨」。我嘗試過,但不會打印出「對不起,沒貨」任何人都可以向我解釋如何正確設置它,所以當項目爲0該程序將讓用戶知道該項目缺貨。謝謝。Java中的其他語句
import java.util.Scanner;
public class VendingMachine {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int Chips = 5;
int Cookies = 4;
int Candies = 3;
double ChipsPrice = 1.25;
double CookiesPrice = 0.85;
double CandiesPrice = 0.95;
Scanner choice = new Scanner(System.in);
Scanner moneyIn = new Scanner(System.in);
while (true) {
double Change = 0;
double Amount = 0;
double Money = 0;
System.out.println("Welcome to the Vending Machine");
System.out.println("Please insert Money");
Amount = moneyIn.nextDouble();
//Make an if statements, such as if moneyIn equals 5 quarters then Amount = 5*0.25
//Ask how many quarters how many nickels how many dimes
System.out.println("What snack would you like?");
System.out.println("Potato Chips: $" + ChipsPrice + " " + Chips + " left");
System.out.println("Cookies: $" + CookiesPrice + " " + Cookies + " left");
System.out.println("Candies: $" + CandiesPrice + " " + Candies + " left");
String which = choice.nextLine();
if (which.equals("Potato Chips")) {
System.out.println("You selected Potato Chips: $" + ChipsPrice + " " + Chips + " left");
if (Amount < ChipsPrice) {
System.out.println("Not enough money inserted");
if (Chips == 0) ;
System.out.println("Sorry, out of stock");
} else {
Chips = Chips - 1;
Change = ChipsPrice - Amount;
System.out.println("Please take your chips ");
System.out.println("Your change is " + Change);
}
} else if (which.equals("Cookies")) {
System.out.println("You selected Cookies: $" + CookiesPrice + " " + Cookies + " left");
Cookies = Cookies - 1;
if (Amount < CookiesPrice) {
System.out.println("Not enough money inserted");
if (Cookies == 0)
System.out.println("Sorry, out of stock");
} else {
Cookies = Cookies - 1;
Change = CookiesPrice - Amount;
System.out.println("Please take your cookies");
System.out.println("Your change is " + Change);
}
} else if (which.equals("Candies")) {
System.out.println("You selected Candies: $" + CandiesPrice + " " + Candies + " left");
if (Amount < CandiesPrice) {
System.out.println("Not enough money inserted");
if (Cookies == 0)
System.out.println("Sorry, out of stock");
} else {
Candies = Candies - 1;
Change = CookiesPrice - Amount;
System.out.println("Please take your candies");
System.out.println("Your change is " + Change);
}
} else {
System.out.println("Please select one of the snacks below");
}
}
}
}
刪除'if(芯片== 0);'末尾的分號。並注意縮進。 – shmosel
除此之外,你需要不同的產品條件,它不能總是'如果(Cookies == 0)'。 –
此外,有時if stmts位於錯誤的地方 – Gab