为什么我的!= 0在while循环中不起作用?

时间:2020-02-27 05:38:24

标签: java while-loop

编写一个模拟收银员终端的程序。假设客户正在购买数量不明的不同商品,每个商品可能有多个数量。使用while循环提示输入单价和数量。循环应继续进行,直到单价为零为止。显示每个项目的小计。循环后显示应付款总额。在适当的地方使用货币格式。

import java.util.Scanner;

public class Program4 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter item price or zero to quit: ");
        double price = input.nextDouble();

        System.out.println("Enter the quantity for this item: ");
        int qty = input.nextInt();

        double Sub_Total = price * qty;

        System.out.printf("Total of this item is $%4.2f", Sub_Total);
        System.out.println();


        double Total = 0;

        while(price != 0) {
            System.out.println("\nEnter item price or zero to quit: ");
            price = input.nextDouble();

            System.out.println("Enter the quantity for this item");
            qty = input.nextInt();

            Sub_Total = price * qty;

            System.out.printf("Total of this item is $%4.2f", Sub_Total);
            System.out.println();


            Total += Sub_Total; 
        }
        System.out.printf("Total is $%5.2f" + Total);

    }

}

我正在为学校编写程序,我无法弄清楚为什么我输入0时while循环没有结束。

4 个答案:

答案 0 :(得分:1)

当您输入0时,循环确实结束;问题是您的println语句不在循环中。您的代码应如下所示:

      ...
      Total += Sub_Total;
      System.out.printf("Total is $%5.2f", Total);
      }
   }
}

如果您希望程序在输入0之后立即结束,则可以添加一条if语句:

Scanner input = new Scanner(System.in);

System.out.println("Enter item price or zero to quit: ");
double price = input.nextDouble();

if (price <= 0) {
    System.out.println("input: zero - program terminated.");
    return; 
}
...

为了使其有效地工作,需要对代码进行重组。还可以对其进行优化以消除冗余;以下应处理情况并正确退出:

import java.util.*;

class Untitled {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    double total = 0;
    double subtotal = 0;
    double qty = 0;

    System.out.println("Enter item price or zero to quit: ");
    double price = input.nextDouble();

    while (price > 0) {

        System.out.println("Enter the quantity for this item: ");
        qty = input.nextDouble();

        subtotal = price * qty;
        total += subtotal;

        System.out.printf("Total of this item is $%4.2f", subtotal);
        System.out.println();

        System.out.println("\nEnter item price or zero to quit: ");
        price = input.nextDouble();

        if (price <= 0)
            break; 
        }
        System.out.printf("Total is $%5.2f", total);
    }
}

As mentioned in the comments遵循惯例,变量名使用小写字母。

答案 1 :(得分:0)

输入价格后,您可以使用价格为0的条件来检查它,可以使用break停止循环

public class Program4 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter item price or zero to quit: ");
        double price = input.nextDouble();

        System.out.println("Enter the quantity for this item: ");
        int qty = input.nextInt();

        double Sub_Total = price * qty;

        System.out.printf("Total of this item is $%4.2f", Sub_Total);
        System.out.println();


        double Total = 0;

        while(price != 0) {
            System.out.println("\nEnter item price or zero to quit: ");
            price = input.nextDouble();

            if(price == 0)
            break;

            System.out.println("Enter the quantity for this item");
            qty = input.nextInt();

            Sub_Total = price * qty;

            System.out.printf("Total of this item is $%4.2f", Sub_Total);
            System.out.println();


            Total += Sub_Total; 
        }
        System.out.printf("Total is $%5.2f",Total);

    }

}

答案 2 :(得分:0)

这应该是为您提供的解决方案,在这里我给出了您的代码有问题的地方的评论

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter item price or zero to quit: ");
        double price = input.nextDouble();

        System.out.println("Enter the quantity for this item: ");
        int qty = input.nextInt();

        double Sub_Total = price * qty;

        System.out.printf("Total of this item is $%4.2f", Sub_Total);
        System.out.println();


        double Total = Sub_Total;  //changed this line to get correct total

        while(price != 0) {
            System.out.println("\nEnter item price or zero to quit: ");
            price = input.nextDouble();

            if(price == 0.0) 
                break;

            System.out.println("Enter the quantity for this item");
            qty = input.nextInt();

            Sub_Total = price * qty;

            System.out.printf("Total of this item is $%4.2f", Sub_Total);
            System.out.println();


            Total += Sub_Total; 

        }
        System.out.printf("Total is $%5.2f", Total); //this also needs to be changed to print output in correct format, 
                                                     //otherwise you will get missing element exception

    }

答案 3 :(得分:-2)

涉及浮点类型(双精度和浮点型)的计算可能不精确,因此通常最好检查一下是否接近于0。请检查以下代码段

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter double val::");
    double d = scanner.nextDouble();

    while (Math.abs(d) <  Double.MIN_VALUE) {
        System.out.println("DoubleTest::d::::" + d);
    }