Question

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");
    }   
Was it helpful?

Solution

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;
    }

OTHER TIPS

change the

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

to

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

and remove

if (preference == 4)
    count = 5;
    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.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top