int dogs;
dogs = Integer.parseInt(JOptionPane.showInputDialog("How many dogs do you have?"));
switch (dogs)
{
case 0: JOptionPane.showMessageDialog(null,"You really should get a dog. They're great pets."); break;
case 1: JOptionPane.showMessageDialog(null,"Glad you have a dog."); break;
case 2: JOptionPane.showMessageDialog(null,"Two dogs are better than one."); break;
case 3: JOptionPane.showMessageDialog(null,"Three dogs is a lot."); break;
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."); break;
default: JOptionPane.showMessageDialog(null,"Invalid input.");
} // end switch
答案 0 :(得分:4)
使用数组。
String[] messages = {
"You really should get a dog...",
...
};
当然,您需要检查数组的边界
if (dogs > 0 && dogs < messages.length) {
JOptionPane.showMessageDialog(null, messages[dogs]);
}
阅读:Arrays