请帮忙解决我卡住的以下代码(我是Java新手)作业要求我在代码中构建一台计算机糖果机。以下是作业输出结果:
Welcome to Shoreline's Computer Candy Machine!
(All candy provided is virtual.)
How much money do you have? > $1.00
$1.00, that's all?
Well, let me tell you what we got here.
A $0.65 Twix
B $0.50 Chips
C $0.75 Nutter Butter
D $0.65 Peanut Butter Cup
E $0.55 Juicy Fruit Gum
So, What do you want? > C
Thanks for purchasing candy through us.
Please take your candy and your $0.25 change!
或者:
Welcome to Shoreline's Computer Candy Machine!
(All candy provided is virtual.)
How much money do you have? > .50
$0.50, that's all?
Well, let me tell you what we got here.
A $0.65 Twix
B $0.50 Chips
C $0.75 Nutter Butter
D $0.65 Peanut Butter Cup
E $0.55 Juicy Fruit Gum
So, What do you want? > D
You are short $0.15, you are unable to purchase your snack
这是我的代码(我还没有完成它):
import java.util.Scanner;
public class CandyMachine {
public static void main(String[] args) {
verse1();
System.out.println();
verse2();
System.out.println();
verse3();
System.out.println();
verse4();
}
public static void verse1() {
System.out.println("Welcome to Shoreline's Computer Candy Machine!");
System.out.println("(All candy provided is virtual.)");
}
public static void verse2() {
Scanner console = new Scanner(System.in);
System.out.print("How much money do you have? >"); //prompts for a whole number
double money = console.nextDouble();
System.out.printf("%.2f, that's all?", money);
}
public static void verse3() {
System.out.println("Well, let me tell you what we got here.");
System.out.println("A $0.65 Twix");
System.out.println("B $0.50 Chips");
System.out.println("C $0.75 Nutter Butter");
System.out.println("D $0.65 Peanut Butter Cup");
System.out.println("E $0.55 Juicy Fruit Gum");
}
public static void verse4() {
Scanner input = new Scanner(System.in);
System.out.print("So, What do you want? >"); //prompts for a whole number
String a = input.next();
if (a.equals("A"))
if (money > 0.65)
System.out.println("Thanks for purchasing candy through us.");
else
}
}
我卡在了verse4()
。我的意思是,我做得对吗?我应该如何在verse2()
中获取“money”并在verse4()
中使用它或者我应该怎么做?请帮帮我。