简体   繁体   中英

I need my variables value to be set by the input of a scanner

Basically lines 32-37 all depend on the variable vendMoney to calculate the AmountDue but its giving me a "local variable may not have been initialized" error because i haven't set the specific value. I want the value to be set to whatever the person inputs into the scanner...

    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
                }

                }
                }
                            }
                }
}
}

I can see a couple of logic errors.

  1. You are calculating the change to give back before you ask for the user's money. You can't know how much money you need to give back before you know how much the user is going to give you. You need to put those calculations after you take the vendMoney input for the user.

  2. By the look of that mess of braces at the end of your class, you have the if-statements nested within each other. In a case such as this, then you want to complete the if-statement with a brace before starting the next one. The way it is now, it probably will only execute the Sprite part if buttonPress == 1 && buttonPress == 2 && buttonPress ==3 . Additionally, in a case where the conditions are logically exclusive as they are here (either 1 or 2 or 3), you probably want to make use of the else if (and/or else) construction . Eg


    if (...) {
      ....
    }
    else if (...) {
      ...
    }
    else if (...) {
      ...
    }
  1. With regard to the question you posed about the "local variable may not have been initialized" error, see this question . In short, you are required to explicitly set the value of local variables before using them. For more detail on variables and scope, read this article from Oracle.

EDIT: As pointed out in the comments below, you do not generally want to end an if-statement with a semicolon. If you type if (condition); {...} if (condition); {...} , the condition will be checked, and then program execution will skip whatever code is contained within the braces regardless of whether the condition is true or false. Leave off the semicolon if you want the code inside the block to execute when the condition is true (so, if (condition) { ... } ). There is no semicolon after the braces.

The same principle is true for loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM