我是java新手。如果用户输入了它不想要的东西,比如一堆随机字母,我正在弄乱一些代码以允许它显示错误而不是整个系统出现错误。我想使用布尔值,但我不知道从哪里开始。我使用了整数的尝试和捕获,但我不确定如何处理布尔值。有人告诉我们一个 equalsIgnoreCase,但我不知道在哪里添加它。非常感谢您的帮助。
import javax.swing.JOptionPane;
public class bday
{
public static void main(String[] args)
{
String age = "0";
age = JOptionPane.showInputDialog("What was your age yesterday?");
int iage = 1;
try
{
iage = Integer.parseInt(age);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Thanks a lot, you broke it. CYA later.");
return;
}
String bday = "0";
bday = JOptionPane.showInputDialog("Was yesterday your B-Day? (True or False)");
try
{
bage = Boolean.parseBoolean(bday);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "WHY U MESS UP PROGRAM???.... BYE BYE!!");
return;
}
if (bage == true){
iage += 1;
JOptionPane.showMessageDialog(null, "You are now " + iage);
}
else if (bage == false){
JOptionPane.showMessageDialog(null, "Happy unbirthday!");
}
if (iage ==10){
JOptionPane.showMessageDialog(null, "Congrats, double digits!");
}
if (iage >19){
JOptionPane.showMessageDialog(null, "U aint a Teenager");
}
else if (iage < 13)
JOptionPane.showMessageDialog(null, "U aint a Teenager");
}
}
我在搞什么...
String bday = "0";
String str1 = "true";
String str2 = "false";
bday = JOptionPane.showInputDialog("Was yesterday your B-Day? (True or False)");
boolean bage = bday.equalsIgnoreCase(str1);
boolean bage2 = bday.equalsIgnoreCase(str2);
if (bage == true){
iage += 1;
JOptionPane.showMessageDialog(null, "You are now " + iage);