-3

I am writting a simple programs using java for my school assignment. I cant find a way to create a infinte loop. Its a survey program which i have to redesign it so it does no only count 5 people but as many as I want untill I end it. Here is a code:

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");
    }   
user3499202
  • 71
  • 1
  • 6
  • 3
    possible duplicate of [Infinite Loop Java](http://stackoverflow.com/questions/13806880/infinite-loop-java) – 0x9BD0 Apr 04 '14 at 17:52
  • possible duplicate of [What does for (;;) mean in Java?](http://stackoverflow.com/questions/7081339/what-does-for-mean-in-java) – donfuxx Apr 04 '14 at 17:53

4 Answers4

1
for (;;)
    {

        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)
        break;
    }
T McKeown
  • 12,971
  • 1
  • 25
  • 32
1

change the

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

to

while (preference != 4)
{
  // code
++count;
}

and remove

if (preference == 4)
    count = 5;
Mike Weber
  • 311
  • 2
  • 16
1
    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");

You must change the loop to infinite and add a break if the answer is 4.

wzona
  • 126
  • 7
1

To make it run indefinetely use

While(true){

}

And when you want it to finish, use the keyword 'break'. This breaks the loops. You could use it in:

if(preference==4){
    break;
}
fersarr
  • 3,399
  • 3
  • 28
  • 35