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
質問する
928 次
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 に答える