糖果机Java

Candy Machine Java

请帮助解决我卡住的以下代码(我是java的新手) 作业说我需要用代码构建一台电脑糖果机。 这是作业的输出:

Welcome to Shoreline's Computer Candy Machine!
(All candy provided is virtual.)

How much money do you have? > .00
.00, that's all?

Well, let me tell you what we got here.
A [=11=].65 Twix
B [=11=].50 Chips
C [=11=].75 Nutter Butter
D [=11=].65 Peanut Butter Cup
E [=11=].55 Juicy Fruit Gum

So, What do you want? > C

Thanks for purchasing candy through us.
Please take your candy and your [=11=].25 change!

或:

Welcome to Shoreline's Computer Candy Machine!
(All candy provided is virtual.)

How much money do you have? > .50
[=12=].50, that's all?

Well, let me tell you what we got here.
A [=12=].65 Twix
B [=12=].50 Chips
C [=12=].75 Nutter Butter
D [=12=].65 Peanut Butter Cup
E [=12=].55 Juicy Fruit Gum

So, What do you want? > D

You are short [=12=].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 [=13=].65 Twix");
   System.out.println("B [=13=].50 Chips");
   System.out.println("C [=13=].75 Nutter Butter");
   System.out.println("D [=13=].65 Peanut Butter Cup");
   System.out.println("E [=13=].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() 中使用它或者我应该怎么做?请帮助我。

在我看来,verse2 中的变量 money 超出了 verse4 的范围。 如果你在一个方法中定义一个变量,它只存在于那个方法中。如果你写它应该工作:

double money;

就在 class 的左括号下方,然后更改

double money = console.nextDouble();

在第 2 节中:

money = console.nextDouble();

您需要将 money 变量声明为全局变量,以便可以在任何地方访问它。它也需要是 static 因为你的方法都是静态的。我确实启动了你的 verse4() 方法,唯一要考虑的是如果用户没有足够的钱......会发生什么?这就是为什么 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 [=10=].65 Twix");
        System.out.println("B [=10=].50 Chips");
        System.out.println("C [=10=].75 Nutter Butter");
        System.out.println("D [=10=].65 Peanut Butter Cup");
        System.out.println("E [=10=].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.
    }