-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");
    }   
2

4 Answers 4

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;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

if (preference == 4) the loop should break.
1

change the

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

to

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

and remove

if (preference == 4)
    count = 5;

1 Comment

I need to do it this way. I have changed it but it doesnt work.
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.