繁体   English   中英

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

[英]why does my !=0 not work in my 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 循环没有结束。

输入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);
    }
}

正如评论中提到的,遵循约定,使用小写字母作为变量名。

输入价格后,您可以使用 if 条件检查它,如果价格为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);

    }

}

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

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

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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