I need my variables value to be set by the input of a scanner
基本上,第32-37行都依赖于变量vendMoney来计算AmountDue,但由于我未设置特定值,因此它给我一个"本地变量可能尚未初始化"错误。我希望将值设置为任何人输入到扫描仪中的值...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 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输入后,需要进行这些计算。
通过类末尾的大括号,您可以看到if语句彼此嵌套。在这种情况下,您需要在开始下一个括号之前先用大括号完成if语句。现在,它可能仅在
1 2 3 4 5 6 7 8 9 | if (...) { .... } else if (...) { ... } else if (...) { ... } |
编辑:正如下面的注释所指出的,您通常不希望用分号结束if语句。如果键入
循环同样的原理。