Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中创建无限循环_Java_Loops_Infinite - Fatal编程技术网

如何在java中创建无限循环

如何在java中创建无限循环,java,loops,infinite,Java,Loops,Infinite,我正在用java编写一个简单的程序来完成我的学校作业。我找不到创建infinte循环的方法。这是一个调查计划,我必须重新设计它,这样它不仅可以计算5个人,而且可以计算到我想要的人数,直到我结束它。下面是一个代码: class morecoffee { public static void main (String [] args) { Scanner input = new Scanner(System.in); int person, pre

我正在用java编写一个简单的程序来完成我的学校作业。我找不到创建infinte循环的方法。这是一个调查计划,我必须重新设计它,这样它不仅可以计算5个人,而且可以计算到我想要的人数,直到我结束它。下面是一个代码:

class morecoffee
{
   public static void main (String [] args)
   {  
        Scanner input = new Scanner(System.in); 

        int person, preference, nothing, sugar, sweetener, count, quit;

        nothing = sugar = sweetener = 0; 
        for (count = 1; count <=5; count++)
        {

            System.out.println ("How do you sweeten your coffee?");
            System.out.println ("1. I don't");
            System.out.println ("2. With sugar?");
            System.out.println ("3. With sweetener?");
            System.out.println ("4. Quit");

            preference = input.nextInt();

            if (preference == 1)
                  nothing++; 

                else if (preference == 2)
                        sugar++;
                else if (preference == 3)
                     sweetener++;

         if (preference == 4)
            count = 5;
        }
        count = count +1;
        System.out.println ("Survey Report");
        System.out.println ("=============");

        System.out.println (nothing + " person don't sweeten coffee");
        System.out.println (sugar + " person use sugar in coffee");
        System.out.println (sweetener + " person use sweetener in coffee");
    }   
class-morecoffee
{
公共静态void main(字符串[]args)
{  
扫描仪输入=新扫描仪(System.in);
int人,偏好,无,糖,甜味剂,计数,退出;
无糖=糖=甜味剂=0;
对于(计数=1;计数更改

for (count = 1; count <=5; count++)
移除

if (preference == 4)
    count = 5;

如果答案为4,则必须将循环更改为无限,并添加一个中断。

以使其不间断运行

While(true){

}
当您希望它完成时,请使用关键字“break”。这会打断循环。您可以在以下情况下使用它:

if(preference==4){
    break;
}

如果(首选项==4)
循环应该中断。我需要这样做。我已经更改了它,但它不起作用。
    int count = 0;
    while(true)
    {
        System.out.println ("How do you sweeten your coffee?");
        System.out.println ("1. I don't");
        System.out.println ("2. With sugar?");
        System.out.println ("3. With sweetener?");
        System.out.println ("4. Quit");

        preference = input.nextInt();

        if (preference == 1) {
              nothing++; 
        } else if (preference == 2) {
              sugar++;
        } else if (preference == 3) {
              sweetener++;
        } else if (preference == 4) {
           break;
        }
        count++;
    }

    System.out.println ("Survey Report");
    System.out.println ("=============");
    System.out.println (nothing + " person don't sweeten coffee");
    System.out.println (sugar + " person use sugar in coffee");
    System.out.println (sweetener + " person use sweetener in coffee");
While(true){

}
if(preference==4){
    break;
}