Java 糖果机
请帮助解决我遇到的以下代码(我是java新手) 作业上说我需要用代码造一台电脑糖果机。 以下是作业的输出:Java 糖果机,java,jgrasp,Java,Jgrasp,请帮助解决我遇到的以下代码(我是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 $
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!
或:
这是我的代码(我还没有完成):
我陷入了
verse4()
的困境。我是说我做对了吗?我如何在verse2()
中提取“钱”并在verse4()
中使用它,或者我应该怎么做?请帮帮我。在我看来,第2节中的变量money
超出了第4节的范围。
如果在方法中定义变量,则该变量仅存在于该方法中。如果你写下:
double money;
就在你们班的开始括号下面,然后换
double money = console.nextDouble();
在第2至第2段中:
money = console.nextDouble();
您需要将
money
变量声明为全局变量,以便可以在任何地方访问它。而且它需要是静态的,因为您的方法都是静态的。我确实启动了你的代码> VIESE4()/Case>方法,唯一需要考虑的是,如果用户没有足够的钱…会发生什么?这就是为什么else{…}
被注释为家庭作业;)!另外,您必须知道=
和
之间的区别。它们在你的计划中至关重要。祝你好运
static double money;//must be a global variable so it could be accessed from all methods.
//you declared it in verse2() method what means that you can ONLY access it in verse2().
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
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();
double change = 0;//the amount of change to give back.
//check which candy they picked as well as if the money is equal or larger than the price.
//not the >= is very important, you only had >.
if (a.equals("A") && money >= 0.65) {
change = money - 0.65;//calculate the change by doing the money - price of candy.
System.out.println("Thanks for purchasing candy through us.");
System.out.println("Please take your candy and your $" + change + " change!");
} else if (a.equals("B") && money >= 0.50) {//same thing for item B, and check the price
change = money - 0.50;
System.out.println("Thanks for purchasing candy through us.");
System.out.println("Please take your candy and your $" + change + " change!");
}//now do items C,D,E in with the same logic.
//now you need to make sure that maybe the user doesn't have enough money...
//you would you the else{...} to prompt to user that the money is not enough.
}
money = console.nextDouble();
static double money;//must be a global variable so it could be accessed from all methods.
//you declared it in verse2() method what means that you can ONLY access it in verse2().
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
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();
double change = 0;//the amount of change to give back.
//check which candy they picked as well as if the money is equal or larger than the price.
//not the >= is very important, you only had >.
if (a.equals("A") && money >= 0.65) {
change = money - 0.65;//calculate the change by doing the money - price of candy.
System.out.println("Thanks for purchasing candy through us.");
System.out.println("Please take your candy and your $" + change + " change!");
} else if (a.equals("B") && money >= 0.50) {//same thing for item B, and check the price
change = money - 0.50;
System.out.println("Thanks for purchasing candy through us.");
System.out.println("Please take your candy and your $" + change + " change!");
}//now do items C,D,E in with the same logic.
//now you need to make sure that maybe the user doesn't have enough money...
//you would you the else{...} to prompt to user that the money is not enough.
}