Reputation: 33
Please help solve the following code I got stuck (i'm new to java) the homework said I need to build a computer candy machine in code. here is the output of the homework:
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!
or:
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
here is my code(i haven't finished it):
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
}
}
I got stuck in verse4()
. I mean am I doing it right? How can I take "money" in verse2()
and use it in verse4()
or what should I do? Please help me.
Upvotes: 3
Views: 1156
Reputation: 973
You need to declare your money
variable as a global variable so it would be accessed anywhere. Also it needs to be static
since your methods are all static. I did start your verse4()
method, the only thing to consider is that if the user doesn't have enough money... what would happen? This is why the else{...}
is commented for homework ;)! Also, you must know the difference between >=
and >
. They are crucial in your program. Good luck.
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.
}
Upvotes: 1
Reputation: 80
It looks to me like the variable money
in verse2 is outside the scope of verse4.
If you define a variable inside a method, it only exists in that method. It should work if you write:
double money;
right under the opening bracket of your class, then change
double money = console.nextDouble();
in verse2 to:
money = console.nextDouble();
Upvotes: 1