2
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
4

1 回答 1

4

使用数组。

String[] messages = {
  "You really should get a dog...",
  ...
};

当然你需要检查数组的边界

if (dogs > 0 && dogs < messages.length) {
   JOptionPane.showMessageDialog(null, messages[dogs]);
}

阅读:数组

于 2013-09-19T22:53:29.497 回答