java - 如何将信息存储在我的浮动中并使其可重复使用?
问题描述
所以基本上,我需要制作一台自动售货机,并且至少需要制作一个浮点数或变量。
它需要记住我投入的金额并在以后使用。
现在,我认为每次启动代码或重复代码时我都会创建一个新钱包。我对编程真的很陌生,我碰了壁。
对不起,如果代码很乱:/:
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
循环之外声明您正在使用的变量。
推荐阅读
- linux - 将 isciadm 配置为在另一个 iscsi-portal 关闭时回退到另一个 iscsi-portal
- batch-file - CMD批处理中的子字符串
- javascript - 为什么函数会修改 Javascript 上参数的值?
- java - 为什么不允许超类final方法和子类方法同名?
- python - 如何将带有盐的 C# 密码散列转换为 windows 的 python
- java - 通过@requestparam Spring 上传多部分文件列表时出错
- amazon-cloudformation - AWS CloudFormation 条件
- python - 如何通过 python 执行这种类型的循环?
- office-scripts - 从 Office 脚本调用 .js 库的函数
- python - 如何根据时间插入位置数据