简体   繁体   English

在Java中按情况中断while循环

[英]Breaking a while loop by case in Java

I need to issue break a while loop if a case is matched in a switch statement. 如果在switch语句中匹配大小写,我需要发出while循环中断。 The case is as follows: if a user enters either a number less than zero or anything greater than five. 情况如下:如果用户输入的数字小于零或大于5。 I have the switch working most of my cost except the two exceptions Ive mentioned. 除上面提到的两个例外外,我的大部分费用都由交换机来承担。 Here is my code: 这是我的代码:

import java.util.Scanner;
public class Product
{
    public static void main(String args[])
    {
        int cntr = 0;
        int product = 0;
        int units = 0;
        double totalcost = 0;
        Scanner MK = new Scanner(System.in);
        cntr=0;
        while (cntr >= 0 && cntr <=5)
        {
            System.out.println("Enter Product no.(1-5) or -1 to Quit");
            product = MK.nextInt();
            switch(product) {

            case 1:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 2.98;
                totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            break;
            case 2:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 4.50;
                totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            break;
            case 3:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 9.98;
                totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            break;
            case 4:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 4.49;
                totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            break;
            case 5:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                product = MK.nextInt();
                double cost = 6.87;
                totalcost = totalcost + (cost*product);
                System.out.println("Current total cost: " + totalcost);
                cntr++;
            }
            case 6:
            {
                System.out.println("Product " + (cntr+1));
                System.out.println("Enter Quantity or -1 to Quit");
                //product = MK.nextInt();
                //double cost = 2.98;
                //totalcost = totalcost + cost*product;
                System.out.println("Current total cost: " + totalcost);
                //cntr++;
            }
            break;
            }
            System.out.println("Total cost-->" +totalcost);
        }
    }
}

If anyone can point me in the right direction on how to stop my program when anything less than zero or greater than 5 is entered I would really appreciate it. 如果有人输入小于零或大于5的任何东西可以指示我如何停止程序,我将不胜感激。

A couple of options: 有两个选择:

  1. Use a flag that you set when you want the loop to end (in your default or -1 clause), or 使用您希望循环结束时设置的标志(在default-1子句中),或者

  2. Use a labelled statement and a directed break in your default (or -1 ) clause 在您的default (或-1 )子句中使用带标签的语句和有针对性的 break

  3. In this specific situation where you said you want the entire program to end, you could use System.exit 在您要结束整个程序的特定情况下,可以使用System.exit

Here's more about option 2: 有关选项2的更多信息:

label: while (...) {          // <== Labelled statement
    switch (...) {
        case ...:
            // ...
            break;            // <=== Normal (undirected) break, just exits what
                              //      it's in (switch in this case)
        // ...
        default:
            break label;      // <=== Directed break, exits loop
    }
}

It's also handy for nested loops. 对于嵌套循环也很方便。


Side note: There's no need for the { and } you have surrounding the statements in your case clauses. 旁注:不需要在case子句中使用{}括住语句。 The code for case continues until the break . case的代码一直持续到break为止。 (Yes, it's a bit different from other statements.) (是的,它与其他语句有点不同。)


Side note 2: You've said Enter Product no.(1-5) or -1 to Quit in your prompt, but you have cases for the values 1 to 6 (inclusive). 旁注2:您已经在提示中说出Enter Product no.(1-5) or -1 to Quit ,但是对于值16 (包括16 ),您都有用例。 You probably meant Enter Product no.(1-**6**) or -1 to Quit . 您可能的意思是Enter Product no.(1-**6**) or -1 to Quit

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

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