Java 我需要通过扫描仪的输入设置变量值
基本上,第32-37行都依赖变量vendMoney来计算AmountDue,但它给了我一个“局部变量可能尚未初始化”错误,因为我没有设置具体的值。我希望该值被设置为任何人输入到扫描仪Java 我需要通过扫描仪的输入设置变量值,java,variables,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 ve
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
}
}
}
}
}
}
}
import java.io.*;
导入java.util.Scanner;
班长{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
扫描仪vendM=新扫描仪(System.in);
int焦炭;
国际焦炭价格;
国际焦炭产量;
国际焦炭库存;
雨露;
国际贸易价格;
int-dewAmountDue;
int dewStock;
智力精灵;
int spritePrice;
int Spritamountdue;
int spriteStock;
int转换焦炭;
int-changeBackDew;
反斯普林特;
国际货币基金组织;
int按钮按下;
焦炭价格=2;
dewPrice=2;
spritePrice=1;
更换焦炭=销售货币-焦炭价格;
changeBackDew=vendMoney-dewPrice;
changeBackSprite=卖方货币-价格;
可口可乐到期日=卖方货币-可口可乐价格;
dewAmountDue=卖方货币-dewPrice;
spriteAmountDue=vendMoney-spritePrice;
焦距=10;
dewStock=10;
spriteStock=10;
System.out.println(“您想要哪种饮料…”);
System.out.println(“”);
System.out.println(“按1表示焦炭”);
System.out.println(“按2表示山露”);
System.out.println(“按3表示精灵”);
buttonPress=scanner.nextInt();
//按键启动
如果(按钮按下==1);
{
System.out.println(“”);
System.out.println(“请输入$2.00”);
vendMoney=scanner.nextInt();
如果(货币=2){
System.out.println(“”);
System.out.println(“这是你的可乐!”);
}
如果(货币>2){
System.out.println(“”);
System.out.println(“您的更改为:$”+changeBackCoke);
}
如果(货币<2){
System.out.println(“”);
System.out.println(“您没有输入正确的金额,请输入:$”+cokeAmountDue);
焦炭库存=焦炭库存-1;
如果(按钮按下==2);
{
System.out.println(“”);
System.out.println(“请输入$2.00”);vendMoney=scanner.nextInt();
如果(货币=2){
System.out.println(“”);
System.out.println(“这是你的山露!”);
如果(货币>2){
System.out.println(“”);
System.out.println(“您的更改为:$”+changeBackDew);
}
如果(货币<2){
System.out.println(“”);
System.out.println(“您没有输入正确的金额,请输入:$”+到期日);
}
如果(按钮按下==3)
{
System.out.println(“”);
System.out.println(“请输入$1.00”);vendMoney=scanner.nextInt();
如果(vendMoney==1){
System.out.println(“”);
System.out.println(“这是你的精灵!”);
}
如果(货币>1){
System.out.println(“”);
System.out.println(“您的更改为:$”+changeBackCoke);
}
如果(货币<1){
System.out.println(“”);
System.out.println(“您没有输入正确的金额,请输入:$”+spriteAmountDue);
}
//按钮按下结束
}
}
}
}
}
}
}
我可以看到一些逻辑错误
buttonPress==1&&buttonPress==2&&buttonPress==3
时执行Sprite部分。此外,如果条件在逻辑上是独占的(1、2或3),则可能需要使用。例如If(条件);{…}
,将检查该条件,然后程序执行将跳过大括号中包含的任何代码,无论该条件是真还是假。如果希望在条件为true时执行块内的代码(因此,if(condition){…}
),请省略分号。brac后面没有分号
if (...) {
....
}
else if (...) {
...
}
else if (...) {
...
}