简体   繁体   中英

Nested While Loops and nested if...else Java

I am having issues with my nested while loops working. The assignment is to ask the user for input on what item they want to purchase and then ask how many of that item they want. The end result is for the total of all items chosen once they no longer want to purchase items.

Just to mention, I wanted to use an array instead of nested if...else statements for the price of the items but we have not learned arrays yet in class.

The loop pauses after input of 1/2/3/4/5 but will successfully terminate if the input is 00.

import java.util.Scanner;
public class Shopping {
     

    public static void main(String[] args){
 Scanner input = new Scanner(System.in);
 //initialize variables
double total = 0; // grand total of items and their amounts
double itemNum = 0;// corresponding item number
int itemAmount = 0;// quantity of item
double itemPrice =0; //price of selected itemNum



//offer user product menu
System.out.printf("The following items are available to purchase: %n"
        + "Item 1: $101.00%nItem 2: $119.25%nItem 3: $160.75%nItem 4: $203.00"
        + "%nItem 5: $109.12%n " );
System.out.printf("/////////////////%n%n");

//prompt user for item they want to buy
        System.out.println("Enter what item you want to purchase as 1, 2, 3, etc. OR "
        + "enter 00 to quit.");
         itemNum = input.nextDouble();
        while (itemNum != 00){
            if (itemNum == 1){
                itemPrice = 101.00;
            }
            else {
                if (itemNum ==2){
                    itemPrice = 119.25;
                }
                else {
                    if (itemNum ==3){
                        itemPrice = 160.75;
                    }
                    else {
                        if (itemNum ==4) {
                            itemPrice = 203.00;
                        }
                        else {
                            if(itemNum ==4){
                        itemPrice = 109.12;
                    }
                        else {
                                if (itemNum == 5){
                                itemPrice = 109.12;
                                }    
        }
              while (itemNum  <= 5){
        System.out.println("Enter how many of the item you want to purchase OR "
        + "enter 00 to quit.");
        itemAmount = input.nextInt();
        while (itemAmount != 00){
       {
        total = itemNum * itemAmount;
        }
        }
        }
                        }
                    }
                }
            }
        }
        if (itemNum == 00){
            System.out.printf("Your total for your items is: $%.2f%n", total);
    }
}
}
    ```

Oof, where to start.

00 doesn't do what you think it does.

00 is an octal number that... is the exact, precise same value as 0. There is no way for a double to represent the difference between a user entering just 0 or 00 . In fact, if the user enters 000.0000 , that's still going to end up being just a 0. Your == 00 code 'works', but suggests something that isn't true. Just write == 0 . If you want the loop to end when the input is precisely 00 and not if it is anything else, you cannot call .nextDouble() . Call .next() , check the string input, and Double.parseDouble it (once you determine it wasn't 00 ) to get to a double, if you must.

Furthermore, the number you're asking for is concrete. Entering "I want item #4.591824" makes no sense. So why are you calling .nextDouble() ? Call .nextInt() .

That's a horrible if/else formatting.

all that you're doing is mapping an item to a price. There are MUCH nicer ways to do this:

// at the top level (as a sibling to your method defs):
private static final Map<Integer, Double> PRICES = Map.of(
    1, 101.00,
    2, 119.25,
    3, 160.75,
    4, 203.00,
    5, 109.12);

// later in a method
double price = PRICES.getOrDefault(itemNum, 0.0);
// price is now the price. or 0.0 if the item wasn't in there.

There. Got rid of the whole ugly mess with one map, which incidentally now is also quite easy to fill off of a data file or a database or whatnot.

If that's a bridge too far for your first steps in java, make a helper method:

public double getPriceForItem(int itemNum) {
    if (itemNum == 1) return 101.00;
    if (itemNum == 2) return 119.25;
    if (itemNum == 3) return 160.75;
    if (itemNum == 4) return 203.00;
    if (itemNum == 5) return 109.12;
}

Your code is unreadable and it already messed you up.

Your code will check if itemNum is 4, and set itemPrice to 203.00. If that's not it, it will check if itemNum is 4, and then set price to 109.12. Which is obviously gobbledygook, and presumably your eyeballs did not see this obvious messup because you've made messy code. That's the cost of messy code, and it should be a good lesson as to why you don't want code your eyeballs can no longer track.

while loops for input

You then start using while loops to ask for a single bit of input (such as 'how many do you want to buy'. That's not what while loops are for.

This code should have exactly 1 while loop. Not 3.

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