-5

New learner in java. Need to know how do I stop while loop from looping? Help plz

public String displayMenu()
{
    while (true)
    {
        System.out.println ("Menu");
        System.out.println ("1. Make Guess");
        System.out.println ("2. List Winner");
        System.out.println ("3. Exit");
    }
}
radoh
  • 4,554
  • 5
  • 30
  • 45
nur
  • 29
  • 1

6 Answers6

4

first of all while(true) is an infinite loop. So you will want to change the conditions I guess you will be saving the chose option as an integer so basically you will be doing something like this would be your best option

int choice = 0;
while(choice !=3)
{
    System.out.println ("Menu");
    System.out.println ("1. Make Guess");
    System.out.println ("2. List Winner");
    System.out.println ("3. Exit");
    choice = keyboard.nextInt();
    //do something with your choice or ignore if it is 3   
}

this answer presumes you are taking input from somewhere as your menu choice

Ronan
  • 583
  • 5
  • 20
  • 1
    Whilst `break;` is the simplest answer to the question, I think this actually leads OP down the correct path. +1 – Tiz Feb 25 '16 at 15:19
2

Add this somewhere in your loop:

if(condition) break;

but essential for breaking a loop is:

break;
2

You can break from the loop using break;

barq
  • 3,681
  • 4
  • 26
  • 39
  • 3
    so you copied and pasted a comment and you got +20 reputation, not fair. – A_Di-Matteo Feb 25 '16 at 15:15
  • @A.DiMatteo I am agree with you. – Bahramdun Adil Feb 25 '16 at 15:23
  • in those cases the suggested approach is to copy and paste the comment, transform it in an answer AND mark the answer as community wiki, so you don't gain reputation on it (fair) but you help the community (and the question) anyway (SO spirit) – A_Di-Matteo Feb 25 '16 at 15:25
1

You'd have to do it like this:

public String displayMenu()
{
    while (true)
    {
        System.out.println ("Menu");
        System.out.println ("1. Make Guess");
        System.out.println ("2. List Winner");
        System.out.println ("3. Exit");
        break;
    }
}

but this is equal to:

public String displayMenu()
    {
         System.out.println ("Menu");
         System.out.println ("1. Make Guess");
         System.out.println ("2. List Winner");
         System.out.println ("3. Exit");
    }

This would display "Menu", "1. Make Guess" [...] once. So according to what you want to print to the screen, you don't even need a loop here.

ParadobC2
  • 72
  • 7
1

I disagree of "breaking" the while loop by using break. I think you will always have and "ending condition" and that's what you should aim for. By using break you are thinking also in not to think about that, although sometimes makes sense for some — I haven't found a single use case where you cannot find a condition to exit.

Using a break statement in your while loop is like having the same statements without it, there has to be some logic you are looking for to repeat the statements you want to repeat.

If you give more context you might get a better result. Moreover, a while (true) is a way to define a infinite loop (something not very useful in real life software).

x80486
  • 6,627
  • 5
  • 52
  • 111
0

With this break; statement your loop will only execute once.

public String displayMenu()
{
    while (true)
    {
        System.out.println ("Menu");
        System.out.println ("1. Make Guess");
        System.out.println ("2. List Winner");
        System.out.println ("3. Exit");
        break;  // break out of loop execution
    }
}
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36