所以基本上,我需要制造一个自动售货机,并且至少需要制造一个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.");
}}
}
}
}
}
}
答案 0 :(得分:2)
我不确定我是否完全理解您的问题,但是您每次使用变量时都在声明它们。
如果您想在程序的整个范围内使用wallet
变量,则只需声明一次,然后将其重新分配为新值即可。
float wallet = 0;
//code...//
wallet = deposit - price;
如果您在每个if
语句中声明一个新变量,即使该变量具有相同的名称(例如,钱包),则超出范围时将不使用该变量。
您应该在while
循环之外声明要使用的变量。