java - 如何将信息存储在 float 中并使其可重复使用?

标签 java

所以基本上,我需要制作一台自动售货机,并且至少需要制作一个 float 或变量。

它需要记住我投入的金额并在以后使用。

现在,我想每次启动代码或重复代码时我都在制作一个新钱包。 我对编程真的很陌生,但我遇到了困难。

抱歉,如果代码困惑:/:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        boolean runmachine = true;
        while (runmachine)
        {
        System.out.println("\n" +
                "Hi, welcome to vending machine #203. What would you like today? A drink?");
        System.out.println("Options: yes and no");



        Scanner scan = new Scanner(System.in);

        String drink = scan.nextLine();
        if (drink.equals("yes")) {
            System.out.println("Would you like your drink cold or hot?");
            String cold_drink = scan.nextLine();

            if (cold_drink.equals("hot")) {
                System.out.println("Options: 1 = Coffee (black), 2 = Tea (green) 3 = Warm water");
            }
            String warm_drinks = scan.nextLine();
            if (warm_drinks.equals("1")) {
                System.out.println("That will be 1.99 - Type PAYH1 to pay");

            }
            String PAYH1 = scan.nextLine();
            if (PAYH1.equals("PAYH1"))
            {
                System.out.println("please enter amount amout of money you wish to desposit, type 0 if not necessary. (in Euro's - accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                Scanner s = new Scanner(System.in);
                float deposit = s.nextInt();
                float price;
                    price  = (float) 1.99;
                float wallet = deposit - price;
                if (wallet < 0)
                {
                    System.out.println("Not enough money!");
                }
                else
                {
                    System.out.println("Thank you for visiting!");
                    System.out.println("You have " + wallet + " euro in exchange.");
                }


            }

            if (warm_drinks.equals("2")) {
                System.out.println("That will be 1.49 - Type PAYH2 to pay");
            }
            String PAYH2 = scan.nextLine();
            if (PAYH2.equals("PAYH2"))
            {
                System.out.println("please enter amount amout of money you wish to desposit, type 0 if not necessary. (in Euro's - accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                Scanner s = new Scanner(System.in);
                float deposit = s.nextInt();
                float price;
                price  = (float) 1.49;
                float wallet = deposit - price;
                if (wallet < 0)
                {
                    System.out.println("Not enough money!");
                }
                else
                {
                    System.out.println("Thank you for visiting!");
                    System.out.println("You have " + wallet + " euro in exchange.");
                }

            }

            if (warm_drinks.equals("3")) {
                System.out.println("That will be 0.40 - Type PAYH3 to pay");
            }
            String PAYH3 = scan.nextLine();
            if (PAYH3.equals("PAYH3"))
            {
                System.out.println("please enter amount amout of money you wish to desposit, type 0 if not necessary. (in Euro's - accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                Scanner s = new Scanner(System.in);
                float deposit = s.nextInt();
                float price;
                price  = (float) 0.40;
                float wallet = deposit - price;
                if (wallet < 0)
                {
                    System.out.println("Not enough money!");
                }
                else
                {
                    System.out.println("Thank you for visiting!");
                    System.out.println("You have " + wallet + " euro in exchange.");
                }

            }


            if (cold_drink.equals("cold"))
            {
                System.out.println("Options: 1 = fanta, 2 = Cola, 3 = Ice Tea");
            }

            String cold_drinks = scan.nextLine();
            if (cold_drinks.equals("1")) {
                System.out.println("That will be 1.99 - Type PAYC1 to pay");
                String PAYC1 = scan.nextLine();
                if (PAYC1.equals("PAYC1"))
                {
                    System.out.println("Please enter amount amout of money you wish to desposit, type 0 if not necessary. (in Euro's - accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                    Scanner s = new Scanner(System.in);
                    float deposit = s.nextInt();
                    float price;
                    price  = (float) 1.99;
                    float wallet = deposit - price;
                    if (wallet < 0)
                    {
                        System.out.println("Not enough money!");
                    }
                    else
                    {
                        System.out.println("Thank you for visiting!");
                        System.out.println("You have " + wallet + " euro in exchange.");
                    }}

                }
            if (cold_drinks.equals("2"))
            {
                System.out.println("That will be 1.49 - Type PAYC2 to pay");
                String PAYC2 = scan.nextLine();
                if (PAYC2.equals("PAYC2"))
                {
                    System.out.println("Please enter amount amout of money you wish to desposit, type 0 if not necessary. (in Euro's - Accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                    Scanner s = new Scanner(System.in);
                    float deposit = s.nextInt();
                    float price;
                    price  = (float) 1.49;
                    float wallet = deposit - price;
                    if (wallet < 0)
                    {
                        System.out.println("Not enough money!");
                    }
                    else
                    {
                        System.out.println("Thank you for visiting!");
                        System.out.println("You have " + wallet + " euro in exchange.");
                    }}
            }
            if (cold_drinks.equals("3"))
            {
                System.out.println("That will be 0.40 - Type PAYC3 to pay");
                String PAYC3 = scan.nextLine();
                if (PAYC3.equals("PAYC3"))
                {
                    System.out.println("Please enter amount amout of money you wish to desposit, type 0 if not necessary. (In Euro's - Accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                    Scanner s = new Scanner(System.in);
                    float deposit = s.nextInt();
                    float price;
                    price  = (float) 1.49;
                    float wallet = deposit - price;
                    if (wallet < 0)
                    {
                        System.out.println("Not enough money!");
                    }
                    else
                    {
                        System.out.println("Thank you for visiting!");
                        System.out.println("You have " + wallet + " euro in exchange.");
                    }}
            }
        }
        if (drink.equals("no")) {
            System.out.println("A meal it is! Would you like it to have meat in it?");
            String vegetarian = scan.nextLine();

            if (vegetarian.equals("no")) {
                System.out.println("Options: 1 = Salad, 2 = Vegetarian pasta 3 = Rice with eggs and vegetables");
            }
            if (vegetarian.equals("1")) {
                System.out.println("That will be 4.99 - Type PAYV1 to pay");
                String PAYV1 = scan.nextLine();
                if (PAYV1.equals("PAYV1"))
                {
                    System.out.println("Please enter amount amout of money you wish to desposit, type 0 if not necessary. -- (In Euro's - Accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                    Scanner s = new Scanner(System.in);
                    float deposit = s.nextInt();
                    float price;
                    price  = (float) 4.99;
                    float wallet = deposit - price;
                    if (wallet < 0)
                    {
                        System.out.println("Not enough money!");
                    }
                    else
                    {
                        System.out.println("Thank you for visiting!");
                        System.out.println("You have " + wallet + " euro in exchange.");
                    }}
            }
            if (vegetarian.equals("2")) {
                System.out.println("That will be 2.49 - Type PAYV2 to pay");
                String PAYV2 = scan.nextLine();
                if (PAYV2.equals("PAYV2"))
                {
                    System.out.println("Please enter amount amout of money you wish to desposit, type 0 if not necessary. -- (in Euro's - Accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                    Scanner s = new Scanner(System.in);
                    float deposit = s.nextInt();
                    float price;
                    price  = (float) 2.49;
                    float wallet = deposit - price;
                    if (wallet < 0)
                    {
                        System.out.println("Not enough money!");
                    }
                    else
                    {
                        System.out.println("Thank you for visiting!");
                        System.out.println("You have " + wallet + " euro in exchange.");
                    }}
            }
            if (vegetarian.equals("3")) {
                System.out.println("That will be 5.29 - Type PAYV3 to pay");
                String PAYV3 = scan.nextLine();
                if (PAYV3.equals("PAYV3"))
                {
                    System.out.println("Please enter amount amout of money you wish to desposit, type 0 if not necessary. -- (In Euro's - Accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                    Scanner s = new Scanner(System.in);
                    float deposit = s.nextInt();
                    float price;
                    price  = (float) 5.29;
                    float wallet = deposit - price;
                    if (wallet < 0)
                    {
                        System.out.println("Not enough money!");
                    }
                    else
                    {
                        System.out.println("Thank you for visiting!");
                        System.out.println("You have " + wallet + " euro in exchange.");
                    }}
            }

            if (vegetarian.equals("yes")) {
                System.out.println("Options: 1 = Salad, 2 = Chicken strips 3 = Burgor");

            }
            String meat = scan.nextLine();
            if (meat.equals("1")) {
                System.out.println("That will be 4.99 - Type PAYM1 to pay");
                String PAYM1 = scan.nextLine();
                if (PAYM1.equals("PAYM1"))
                {
                    System.out.println("Please enter amount amout of money you wish to desposit, type 0 if not necessary. -- (In euro's - Accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                    Scanner s = new Scanner(System.in);
                    float deposit = s.nextInt();
                    float price;
                    price  = (float) 4.99;
                    float wallet = deposit - price;
                    if (wallet < 0)
                    {
                        System.out.println("Not enough money!");
                    }
                    else
                    {
                        System.out.println("Thank you for visiting!");
                        System.out.println("You have " + wallet + " euro in exchange.");
                    }}

            }
            if (meat.equals("2")) {
                System.out.println("That will be 2.49 - Type PAYM2 to pay");
                String PAYM2 = scan.nextLine();
                if (PAYM2.equals("PAYM2"))
                {
                    System.out.println("Please enter amount amout of money you wish to desposit, type 0 if not necessary. -- (In Euro's - accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                    Scanner s = new Scanner(System.in);
                    float deposit = s.nextInt();
                    float price;
                    price  = (float) 2.49;
                    float wallet = deposit - price;
                    if (wallet < 0)
                    {
                        System.out.println("Not enough money!");
                    }
                    else
                    {
                        System.out.println("Thank you for visiting!");
                        System.out.println("You have " + wallet + " euro in exchange.");
                    }}
            }
            if (meat.equals("3")) {
                System.out.println("That will be 5.29 - Type PAYM3 to pay");
                String PAYM3 = scan.nextLine();
                if (PAYM3.equals("PAYM3"))
                {
                    System.out.println("Please enter amount amout of money you wish to desposit, type 0 if not necessary. -- (In euro's - Accepted coins: 1 euro, 2 euro, 5 euro bill and 10 euro bill)");
                    Scanner s = new Scanner(System.in);
                    float deposit = s.nextInt();
                    float price;
                    price  = (float) 5.29;
                    float wallet = deposit - price;
                    if (wallet < 0)
                    {
                        System.out.println("Not enough money!");
                    }
                    else
                    {
                        System.out.println("Thank you for visiting!");
                        System.out.println("You have " + wallet + " euro in exchange.");
                    }}
            }



        }
    }
}
}

最佳答案

我不确定我是否完全理解您的问题,但您每次使用变量时都会声明它们。

如果您想在整个程序范围内使用wallet变量,您只需声明一次,然后将其重新分配为新值。

float wallet = 0;
//code...//
wallet = deposit - price;

如果您在每个 if 语句中声明一个新变量,即使它具有相同的名称(例如 wallet),那么当它超出范围时也不会被使用。

您应该在 while 循环之外声明您使用的变量。

关于java - 如何将信息存储在 float 中并使其可重复使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53308513/

相关文章:

java - 如果我是盲人,我如何在 Eclipse IDE for Java 中查看错误指示器

java - 将多个对象写入包

Java 将 JPanel 高度设置为窗口高度的百分比

java - 在 ListView 中移动 ImageView

Java - Lock 如何保证 happens-before 关系?

java - 在 JShell 上执行时同一语句的不同行为

java - org.quartz.jobStore.clusterCheckinInterval 的推荐值

Java:将列表分成n个随机长度的部分

Java:不会编译 - 必须兼容的不兼容类型

java - 如何在swing中自由布局动态添加按钮等组件?