【问题标题】:Candy Machine Java糖果机 Java
【发布时间】: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() 中使用它,或者我该怎么办?请帮帮我。

【问题讨论】:

    标签: java jgrasp


    【解决方案1】:

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

    double money;
    

    就在您班级的左括号下方,然后更改

    double money = console.nextDouble();
    

    在第 2 节中:

    money = console.nextDouble();
    

    【讨论】:

      【解决方案2】:

      您需要将您的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 $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.
          }
      

      【讨论】:

        猜你喜欢
        • 2022-07-05
        • 2022-07-03
        • 2021-12-17
        • 2022-06-26
        • 1970-01-01
        • 2019-11-13
        • 2011-04-21
        • 2022-12-18
        • 1970-01-01
        相关资源
        最近更新 更多