我需要通过扫描仪的输入设置我的变量值

时间:2015-02-24 21:31:31

标签: java variables

基本上第32-37行都依赖于变量vendMoney来计算AmountDue,但它给我一个"局部变量可能尚未初始化"错误,因为我没有设置具体的值。我希望将值设置为人输入扫描仪的任何内容......

    import java.io.*;
import java.util.Scanner;
class Main {

public static void main (String[] args) {


Scanner scanner = new Scanner(System.in);
Scanner vendM = new Scanner(System.in);
int coke;
int cokePrice;
int cokeAmountDue;
int cokeStock;
int dew;
int dewPrice;
int dewAmountDue;
int dewStock;
int sprite;
int spritePrice;
int spriteAmountDue;
int spriteStock;
int changeBackCoke;
int changeBackDew;
int changeBackSprite;
int vendMoney;
int buttonPress;


cokePrice = 2;
dewPrice = 2;
spritePrice = 1;
changeBackCoke = vendMoney - cokePrice;
changeBackDew = vendMoney - dewPrice;
changeBackSprite = vendMoney - dewPrice;
cokeAmountDue = vendMoney - cokePrice;
dewAmountDue = vendMoney - dewPrice;
spriteAmountDue = vendMoney - spritePrice;
cokeStock = 10;
dewStock = 10;
spriteStock = 10;


        System.out.println("Which drink would you like...");
        System.out.println(" ");
        System.out.println("Press 1 for Coke");
        System.out.println("Press 2 for Mountain Dew");
        System.out.println("Press 3 for Sprite");
        buttonPress = scanner.nextInt();

//button presses start
            if (buttonPress == 1);
                {
                    System.out.println(" ");
                    System.out.println("Please enter $2.00");
                        vendMoney = scanner.nextInt();
                            if (vendMoney == 2){
                                System.out.println(" ");
                                System.out.println("Here is your coke!");
                                }
                            if (vendMoney > 2){
                                System.out.println(" ");
                                System.out.println("Your change is: $" + changeBackCoke);
                                }
                            if (vendMoney < 2){
                                System.out.println(" ");
                                System.out.println("You didn't enter the correct amount of money please enter: $" + cokeAmountDue);

                                    cokeStock = cokeStock -1;



            if (buttonPress == 2);
                {
                    System.out.println(" ");
                    System.out.println("Please Enter $2.00");                       vendMoney = scanner.nextInt();
                            if (vendMoney == 2){
                            System.out.println(" ");
                            System.out.println("Here is your Mountain Dew!");
                            if (vendMoney > 2){
                                System.out.println(" ");
                                System.out.println("Your change is: $" + changeBackDew);
                                }
                            if (vendMoney < 2){
                                System.out.println(" ");
                                System.out.println("You didn't enter the correct amount of money please enter: $" + dewAmountDue);
                                }



            if (buttonPress == 3)
                {
                    System.out.println(" ");
                    System.out.println("Please Enter $1.00");                       vendMoney = scanner.nextInt();
                            if (vendMoney == 1){
                            System.out.println(" ");
                            System.out.println("Here is your Sprite!");
                                }
                            if (vendMoney > 1){
                                System.out.println(" ");
                                System.out.println("Your change is: $" + changeBackCoke);
                                }
                            if (vendMoney < 1){
                                System.out.println(" ");
                                System.out.println("You didn't enter the correct amount of money please enter: $" + spriteAmountDue);
                                }
//button presses end
                }

                }
                }
                            }
                }
}
}

1 个答案:

答案 0 :(得分:2)

我可以看到一些逻辑错误。

  1. 您正在计算更改以在您要求用户的钱之前回馈。在您知道用户将给您多少钱之前,您无法知道需要多少钱。在为用户获取vendMoney输入后,您需要进行这些计算。

  2. 通过在课程结尾处看到那些乱七八糟的大括号,你可以将if语句嵌套在一起。在这种情况下,您希望在开始下一个之前用括号完成if语句。它现在的方式,它可能只会buttonPress == 1 && buttonPress == 2 && buttonPress ==3执行Sprite部分。此外,如果条件在逻辑上是独占的(例如1或2或3),您可能想要使用else if (and/or else) construction。 E.g。

  3. 
        if (...) {
          ....
        }
        else if (...) {
          ...
        }
        else if (...) {
          ...
        }
    
    1. 关于你提出的关于&#34;局部变量的问题可能尚未初始化&#34;错误,请参阅this question。简而言之,您需要在使用之前显式设置局部变量的值。有关变量和范围的更多详细信息,请阅读Oracle的this article
    2. 编辑:正如下面的评论中所指出的,您通常不希望以分号结束if语句。如果键入if (condition); {...},将检查条件,然后程序执行将跳过大括号中包含的任何代码,无论条件是true还是false。如果您希望块条件内的代码在条件为真时执行,则不要使用分号(因此,if (condition) { ... })。大括号后没有分号。

      循环也是如此。