嵌套While循环和if ... else嵌套

时间:2020-09-21 01:42:01

标签: java while-loop

我的嵌套while循环无法正常工作。 任务是要求用户输入他们要购买的商品,然后询问他们要购买多少商品。最终结果是选择了不再需要购买的所有物品的总数。

只需提一下,我想使用数组而不是嵌套的if ... else语句来表示商品价格,但我们还没有在课堂上学习数组。

输入1/2/3/4/5后,循环暂停,但如果输入为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);
    }
}
}
    ```

1 个答案:

答案 0 :(得分:1)

Oof,从哪里开始。

00并没有您认为的那样。

00是一个八进制数字,...是与0完全相同的精确值。无法使用双精度表示用户仅输入0或{{ 1}}。实际上,如果用户输入00,那么最终还是只能是0。您的000.0000代码“有效”,但提示不正确。只需写== 00。如果您希望循环在输入恰好为 == 0时结束,而不是其他任何输入,则不能调用00。调用.nextDouble(),检查字符串输入,然后.next()(一旦确定不是Double.parseDouble,则必须加倍)。

此外,您要输入的数字是具体的。输入“我想要项目#4.591824”没有任何意义。那么,为什么要致电00?致电.nextDouble()

如果/否则格式很糟糕。

您要做的就是将商品映射到价格。有很多更好的方法可以做到这一点:

.nextInt()

在那里。一张地图消除了整个丑陋的混乱局面,顺便说一句,现在也很容易填写数据文件,数据库或其他内容。

如果这对于您使用Java的第一步没有太大帮助,请创建一个辅助方法:

// 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.

您的代码不可读,已经把您搞砸了。

您的代码将检查itemNum是否为4,并将itemPrice设置为203.00。如果不是,它将检查itemNum是否为4,然后将price设置为109.12。显然这是傻瓜,大概是因为您编写了混乱的代码,所以您的眼球没有看到这种明显的混乱。那就是乱码的代价,对于为什么不想让您的眼球无法追踪的原因,这应该是一个很好的教训。

while循环输入

然后,您开始使用 while 循环来请求一点输入(例如“您想购买多少”。这不是while循环的作用。

此代码应恰好具有1个while循环。不是3。