Reputation: 55
What piece of code do I need to make it so that if the user enters 7, 8, or 9 dogs; it will still output the message in case 6?
int dogs;
dogs = Integer.parseInt(JOptionPane.showInputDialog("How many dogs do you have?"));
switch (dogs)
{
...
...
...
case 4: JOptionPane.showMessageDialog(null,"Four dogs is too many."); break;
case 5: JOptionPane.showMessageDialog(null,"Five dogs means you're a crazy person."); break;
case 6: JOptionPane.showMessageDialog(null,"That is totally unbelieveable.");
default: JOptionPane.showMessageDialog(null,"Invalid input.");
} // end switch
Upvotes: 1
Views: 3316
Reputation: 2670
import java.io.*;
import java.lang.*;
import java.util.*;
public class DogCheck {
public static void main(String[] args) {
int dogs;
Scanner input = new Scanner(System.in);
System.out.println("Enter Number of Dogs :");
dogs=input.nextInt();
if (dogs < 0)
System.out.println("WoW! Aliens has arrived...")
else
switch(dogs) {
case 4: JOptionPane.showMessageDialog(null,"Four dogs is too many."); break;
case 5: JOptionPane.showMessageDialog(null,"Five dogs means you're a crazy `person."); break;`
default:
JOptionPane.showMessageDialog(null,"That is totally unbelieveable.");
break;
}
}
}
Upvotes: 0
Reputation: 6873
case 6:
case 7:
case 8:
JOptionPane.showMessageDialog(null,"That is totally unbelieveable.");
break;
This will do the job.
However I'd change it to:
case 4: JOptionPane.showMessageDialog(null,"Four dogs is too many."); break;
case 5: JOptionPane.showMessageDialog(null,"Five dogs means you're a crazy person."); break;
default: JOptionPane.showMessageDialog(null,"That is totally unbelieveable.");break;
This will eliminate the Invalid Output message, but will work for each and every number > 5, but i think that is acceptable, since the value of dogs comes out from an Integer.parseInt()
call. If content is invalid an exceptin will be thrown there, and the Invalid Input message can be showed inside the exception handler and like wise an exception can be thrown if dogs is negative.
This has the advantage that will work for every number of dogs. If in need to manage a different error message, it will be enough just add the specific case
branch.
int dogs;
try {
dogs = Integer.parseInt(JOptionPane.showInputDialog("How many dogs do you have?"));
if (dogs < 0) {
throw new Exception("Negative dog is impossible!");
}
switch (dogs)
{
...
...
...
case 4: JOptionPane.showMessageDialog(null,"Four dogs is too many."); break;
case 5: JOptionPane.showMessageDialog(null,"Five dogs means you're a crazy person."); break;
default: JOptionPane.showMessageDialog(null,"That is totally unbelieveable.");break;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Invalid input.");
}
Upvotes: 3
Reputation: 69369
Check for an invalid number, then just use a default
clause:
if (dogs < 0) {
JOptionPane.showMessageDialog(null,"Invalid input.");
} else {
switch(dogs) {
// ...
case 5:
JOptionPane.showMessageDialog(null,"Five dogs means you're a crazy person.");
break;
default:
JOptionPane.showMessageDialog(null,"That is totally unbelieveable.");
break;
}
}
Upvotes: 4
Reputation: 68715
Simply add cases for 7,8,9 with 6
case 6:
case 7:
case 8:
case 9:
JOptionPane.showMessageDialog(null,"That is totally unbelieveable.");
break;
Upvotes: 3