java - Java中的if else语句

标签 java

需要有关 Java 中 if else 语句的帮助。当商品为 0 时,需要程序说“抱歉,缺货”。我尝试过,但它不会打印出“抱歉,缺货”。任何人都可以向我解释如何在商品为 0 时正确设置它该程序会让用户知道该商品缺货。谢谢。

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

最佳答案

为了回顾这一点,我有一些观察:

// It might be simpler to use a "switch" statement here
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");
             // Remove the semicolon - as written this won't do anything
             // Also, this condition shouldn't be here since you're not vending anyway
             // Incidentally, many people argue that you should always use curly
             // brackets, even around one-line "if" statements like this, precisely
             // to prevent errors like this
             if (Chips == 0);
             System.out.println("Sorry, out of stock");

         }
         else {
             // This can be written as Chips--;
             Chips = Chips - 1;
             // Should actually be Amount - ChipsPrice;
             // If they paid 75 cents for a 25-cent item, the change is 75 - 25 = 50 cents,
             // NOT 25 - 75 = -50 cents
             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 = Cookies - 1;

         if (Amount < CookiesPrice){
             System.out.println("Not enough money inserted");

             // Should be checked in the "else" statement
             if (Cookies == 0)
                 System.out.println("Sorry, out of stock");
         }
         else {
             // Cookies--
             Cookies = Cookies - 1;
             // Amount - CookiesPrice
             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");
             // Again, you shouldn't check this here given that you won't vend either way
             // Also, should be if (Candies == 0), NOT if (Cookies == 0)
             if (Cookies == 0)
                 System.out.println("Sorry, out of stock");
         }
         else {
             // Candies--;
             Candies = Candies - 1;
             // Should actually be Amount - CandyPrice. You use CookiesPrice instead.
             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");
     }

还有一件事:你基本上连续 3 次做同样的事情;在这种情况下,通常最好尝试将有问题的行为重构为方法(而不是单独键入 3 次)。

关于java - Java中的if else语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42127696/

相关文章:

java - Spring Boot Mongo Upsert 数组中的元素

java - 如何在 Mac 上安装 Java 8

java - 将 PostgreSQL JSON 列映射到 Hibernate 实体属性

java - 相等元素和树集

java - Cassandra 查询偶尔失败

java - mysql 加载以前的查询数据来呈现查询

java - <h :selectOneMenu> NullPointerException getAsObject method

java - 使用扩展现有类文件的 intellij(JAXB 插件)从 XSD 生成 java 类

java - IntelliJ IDEA 不会在调试时将应用程序服务器描述符和静态 Web 文件复制到其临时文件夹

java - 在队列中添加元素时,调用监听器来通知队列元素是可变的