【问题标题】:I need my variables value to be set by the input of a scanner我需要通过扫描仪的输入设置我的变量值
【发布时间】:2015-02-24 21:31:31
【问题描述】:

基本上第 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
                }

                }
                }
                            }
                }
}
}

【问题讨论】:

  • 我们这里没有行号,您必须指出您所指的位置。另外,你的问题是什么?也许您只需要将这些计算移到设置 vendMoney 的位置之外。
  • 在 Java 中,方法中的代码从上到下顺序执行。在几个地方,你使用了vendMoney你给它赋值。这是不允许的。每次更改 vendMoney 时,说 spriteAmountDue = vendMoney - spritePrice 不会自动重新计算。该计算发生一次,在计算机遇到那条线的那一刻。

标签: java variables


【解决方案1】:

我可以看到几个逻辑错误。

  1. 在要求用户付款之前,您正在计算要返还的零钱。在您知道用户将给您多少之前,您无法知道您需要回馈多少钱。您需要在为用户输入 vendMoney 后进行这些计算。

  2. 从课堂末尾那一堆乱七八糟的大括号来看,你有相互嵌套的 if 语句。在这种情况下,您希望在开始下一个语句之前用大括号完成 if 语句。现在的方式,它可能只会在buttonPress == 1 &amp;&amp; buttonPress == 2 &amp;&amp; buttonPress ==3 时执行Sprite 部分。此外,如果条件在逻辑上是排他的(1 或 2 或 3),您可能希望使用else if (and/or else) construction。例如


    if (...) {
      ....
    }
    else if (...) {
      ...
    }
    else if (...) {
      ...
    }
  1. 关于您提出的关于“局部变量可能尚未初始化”错误的问题,请参阅this question。简而言之,您需要在使用局部变量之前显式设置它们的值。有关变量和范围的更多详细信息,请阅读 Oracle 的 this article

编辑:正如下面的 cmets 所指出的,您通常不希望用分号结束 if 语句。如果您键入if (condition); {...},将检查条件,然后程序执行将跳过大括号中包含的任何代码,无论条件是真还是假。如果您希望在条件为真时执行块内的代码(例如,if (condition) { ... }),请不要使用分号。大括号后面没有分号。

循环也是如此。

【讨论】:

  • 也许在他的if 语句之后但在块开始之前添加一些关于分号的内容。
  • 这也是一个很好的观点,尽管我认为在此之前他还有很多需要解决的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多