糖果机Java

3
请帮忙解决我卡住的以下代码(我是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()中获取“money”并在verse4()中使用它或者我应该怎么做?请帮帮我。

2个回答

1

在我的看法中,verse2中的变量money超出了verse4的作用域。 如果你在一个方法内定义一个变量,它只存在于该方法中。如果你这样写应该可以解决:

double money;

在你的类的开头括号下方,然后更改


double money = console.nextDouble();

在第二节诗歌中:
money = console.nextDouble();

1

你需要将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.
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接