【发布时间】:2018-07-21 02:12:57
【问题描述】:
请帮助解决我卡住的以下代码(我是 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() 中取“钱”并在verse4() 中使用它,或者我该怎么办?请帮帮我。
【问题讨论】: