简体   繁体   中英

How can I get this while loop to run when using a sentinel value for a variable with scanner input inside of the loop, while calling methods?

2 months into my first java programming. I'm trying to create a while loop in a class with methods and scanner inputs. Attempting to use a sentinel value to break out of the while loop. I've attempted while loops, do-while loops, with and without if-else blocks.

It looks like it's requiring me to initialize the salePrice for it to work, but the salePrice is entered by the user inside the loop. I have tried to assigning a fake value to it before the while loop, it then lets me start the loop, but 0 does not break of out of it, it just goes to the second question in the loop.

Is there something fundamentally wrong with this logic structure? Do the methods create a different need? I'm only 2 months into coding, so it has to be a very simple answer... Do I need to put if-else logic within the while loop just to break out of the loop with a sentinel value in this case?

public class PurchaseCalculator {

    //create main method
    public static void main(String[] args) {
        // declare variables
        // double salePrice;
        double discount = .15;
        double maxPurchaseAmount;
        double salePrice;

        // create scanner object
        Scanner userInput = new Scanner(System.in);
       
        while (salePrice > 0){
            // prompt user to enter price
            System.out.println("Enter the price of the item. Enter 0 to end program"); 
            // read in price from user and store in sale price
            salePrice = userInput.nextDouble();
        
            //create a scanner object for salesPrice input
            System.out.println("Price entered: $" + salePrice);
            //prompt user to enter max purchase amount
            System.out.println("Enter the maximum purchase amount");
            // read in max amount from user and store in maxPurchaseAmount
            maxPurchaseAmount = userInput.nextDouble();
            
            // call displaypricewithtax method
            displayPriceWithTax(salePrice);
            // call computePriceAfterDiscountMethod
            computePriceAfterDiscount(salePrice, discount);
            // call displaypurchasablelimit method
            displayPurchasableNumber(salePrice, maxPurchaseAmount);
        
        } // close while loop
    } //close main

In Java any local variable 1 must be initialized before its value can be read. Not to be confused with class fields 2 , which are initialized with default values.

1 - variables declared inside statement blocks ( {...} )
2 - instance variables or class variables


Option 1: change the while loop to a do-while loop. Using this, the check is done after the iteration, so a value is assigned to the variable:

double salePrice;

do {
    ...
    salePrice = userInput.nextDouble();
    ...
while (salePrice > 10);

To avoid execution of the rest of the iteration, an if can be used; but mostly the next option (Option 2) is used instead:

double salePrice;

do {
    ...
    salePrice = userInput.nextDouble();
    if (salePrice >0) {
        ...
    }
while (salePrice > 10);

Option 2: use an if inside the loop:

while (true) {    // for ever
    ...
    salePrice = userInput.nextDouble();
    if (salePrice <= 0)
        break;
}

Option 3: use a flag ( boolean ) to control the loop

boolean stop = false;
while (!stop) {
    ...
    salePrice = userInput.nextDouble();
    stop = salePrice <=0;
    ...
}

Option 4: assign a fake initial value:

double salePrice = 1.0;
while (salePrice > 0) {
    ...
    salePrice = userInput.nextDouble();
    ...
}

(I would suggest going by first or second option, eventually 3rd for some cases)

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