public class SwitchErrorsGui { public static void main(String[] args) { String i
ID: 3554931 • Letter: P
Question
public class SwitchErrorsGui
{
public static void main(String[] args)
{
String input:
JOptionPane.showMessageDialog(null,"Key :1=blue, 2=red, 3=green");
input = JOptionPane.showInputDialog("Enter a number and I'll return the corresponding color");
int number= Integer.parseInt(input);
switch(number)
{
case 1:
JOptionPane.showMessageDialog(null,"You choose red! ");
break;
case 2:
JOptionPane.showMessageDialog(null,"You choose blue! ");
break;
case 3:
JOptionPane.showMessageDialog(null,"You choose green! ");
break;
default:
JOptionPane.showMessageDialog(null,"Color not available! ");
break;
}
System.exit(0);
}
}
Explanation / Answer
import javax.swing.JOptionPane;
public class SwitchErrorsGui {
public static void main(String[] args) {
String input;
JOptionPane.showMessageDialog(null, "Key :1=blue, 2=red, 3=green");
input = JOptionPane.showInputDialog("Enter a number and I'll return the corresponding color");
int number = Integer.parseInt(input);
switch (number) {
case 1:
JOptionPane.showMessageDialog(null, "You choose red! ");
break;
case 2:
JOptionPane.showMessageDialog(null, "You choose blue! ");
break;
case 3:
JOptionPane.showMessageDialog(null, "You choose green! ");
break;
default:
JOptionPane.showMessageDialog(null, "Color not available! ");
break;
}
System.exit(0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.