How do I loop a switch Statement?
How do I loop a switch Statement?
I need to loop it so that when a user inputs a 1, 2, 3, or anything other than a 4 it restarts, and when the user types in a 4 the program terminates.
Code Java:
import javax.swing.JOptionPane ;
public class Input123
{
public static void main(String[] args)
{
int n;
n = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter a 1, 2, 3, or 4" )) ;
{
switch(n)
{
case 1:
case 2:
case 3:
System.out.println("Good Job!") ;
break ;
case 4:
System.out.println("Pragram Terminated") ;
break ;
default:
System.out.println("No such Parameter") ;
}
}
}
Re: How do I loop a switch Statement?
What you can do is place your switch inside a while loop => while(flag). Then inside the case 4 set the flag to false.
Re: How do I loop a switch Statement?
Quote:
Originally Posted by
Junky
What you can do is place your switch inside a while loop => while(flag). Then inside the case 4 set the flag to false.
Can you elaborate?
Re: How do I loop a switch Statement?
Use a boolean.
Code java:
boolean foo = true;
while(foo) {
//stuff
if( x == 4) {
foo = false;
}
}
Above example uses an if but you can easily apply it to a switch
Re: How do I loop a switch Statement?
Quote:
Originally Posted by
Junky
Use a boolean.
Code java:
boolean foo = true;
while(foo) {
//stuff
if( x == 4) {
foo = false;
}
}
Above example uses an if but you can easily apply it to a switch
It's compiles with no syntax errors, but it's still givng me an infinite loop where when I put in a 1, 2, 3, or any number other not 4 it outputs either "Good Job!" or "Parameter Error". So I probally have it wrong somewhere.
Code java:
import javax.swing.JOptionPane ;
public class Input123
{
public static void main(String[] args)
{
int n;
n = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter a 1, 2, 3, or 4" )) ;
{
boolean loop = true;
while(loop)
{
switch(n)
{
default:
System.out.println("Parameter Error") ;
break ;
case 1:
System.out.println("Good Job!") ;
break;
case 2:
System.out.println("Good Job!") ;
break;
case 3:
System.out.println("Good Job!") ;
break ;
case 4:
System.out.println("Pragram Terminated") ;
break ;
}
if(n == 4)
{
loop = false;
}
}
}
}
}
Re: How do I loop a switch Statement?
I suggest you put the input dialog inside the loop, so it prompts for input each time round...
Incidentally, by convention, 'default' is the last item in a 'switch', the idea being that all values needing individual handling are checked first, and the default takes effect for all other values. This also means that by omitting the 'break' immediately above, execution will 'drop through' to the default handler.
Re: How do I loop a switch Statement?
Yea he's right. You definitely need the input inside the while loop or its going to only ask once and keeping going with that same input forever.
Re: How do I loop a switch Statement?
What you want to do is move the loop so the input and the switch statement is contained within it.
Easiest way would be to make it a while loop with the condition true, but give the loop a label and to exit just break that loop as I have edited your code to do below.
Code :
[highlight = Java]
edited by moderator - please read http://www.javaprogrammingforums.com/cafe/9544-problem-spoon-feeding.html
[/highlight]