当我编译时,我收到一条错误消息,说“消息”尚未初始化。我想要完成的不是多个 JOptionPane.showMessageDialog 语句,而是我的代码中的一个语句,它将根据用户输入输出下面列出的任何消息。
int dogs;
String message;
dogs = Integer.parseInt(JOptionPane.showInputDialog("How many dogs do you have?"));
if (dogs >= 6)
{
message = "That is totally unbelieveable.";
}
else
{
switch (dogs)
{
case 0: message = "You really should get a dog. They're great pets."; break;
case 1: message = "Glad you have a dog."; break;
case 2: message = "Two dogs are better than one."; break;
case 3: message = "Three dogs is a lot."; break;
case 4: message = "Four dogs is too many."; break;
case 5: message = "Five dogs means you're a crazy person."; break;
JOptionPane.showMessageDialog(null,message);
default: JOptionPane.showMessageDialog(null,"Invalid input."); break;
} // end switch
} // end if