我正在使用jcreator。它说过程完成但是当你去切换然后案例4然后选择数字1-9时,它说java.numberformatexception。造成这种情况的原因是什么?如何解决?我只是个初学者。
import javax.swing.JOptionPane;
public class Exer29A {
public static String inputdialog(String s)
{
JOptionPane.showInputDialog(null,s);
return s;
}
static void messagedialog(String r)
{
JOptionPane.showMessageDialog(null, r);
}
public static void main(String[] args) {
int xx, x1, x2;
String n1, n2, LName, MName, FName;
FName = JOptionPane.showInputDialog(null, "What's your first name?");
MName = JOptionPane.showInputDialog(null, "What's your Middle name?");
LName = JOptionPane.showInputDialog(null, "What's your Last name?");
n2 = JOptionPane.showInputDialog(null, "So you are " + FName.substring(0,1).toUpperCase() + FName.substring(1).toLowerCase() + " " + MName.substring(0,1).toUpperCase() + MName.substring(1).toLowerCase() + " " + LName.substring(0,1).toUpperCase() + LName.substring(1).toLowerCase() + " " + "Do you wish to continue?[Y/N]");
if (n2.equalsIgnoreCase("Y"))
{
x1 = Integer.parseInt(JOptionPane.showInputDialog(null, "What do you want to do? \n 1. Add 2 integers \n 2. Area of a rectangle \n 3. Area of a triangle \n 4. Know your fate."));
switch (x1)
{
case 1:
{
x1 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1st integer."));
x2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 2nd integer"));
messagedialog("The sum of two numbers is " + (x1+x2) );
}
break;
case 2:
{
x1 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1st integer."));
x2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 2nd integer"));
messagedialog("The area of the triangle is " + (x1*x2) );
}
break;
case 3:
{
x1 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter base."));
x2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter height"));
messagedialog("The are of the triangle is " + ((double)(0.5*x1*x2)));
}
case 4:
{
xx = Integer.parseInt(inputdialog("Choose numbers 1-9"));
{
if (xx==1)
messagedialog("You will be successful!");
else if (xx==2)
messagedialog("You will have a beautiful life. Blessings would come to you.");
else if (xx==3)
messagedialog("You are handsome/gorgeous. But.....");
else if (xx==4)
messagedialog("You must study well.");
else if (xx==5)
messagedialog("Unfortunately, you are unfortunate.");
else if (xx==6)
messagedialog("You will be an environmentalist. You will be called the Keeper of the Forest.");
else if (xx==7)
messagedialog("You are nothing but luck. You will be poured out with luck.");
else if (xx==8)
messagedialog("8. Nothing but a number. Try again.");
else
messagedialog("Endowed with the gift to serve others, people with the lucky number 9 are able to freely create an easy and relaxed atmosphere. /n Your humanity is welcomed among their friends. Besides, they are brilliant, funny, smart and generous. ");
}
}
break;
default:
messagedialog("invalid");
}
}
else if (n2.equalsIgnoreCase("N"))
System.exit(0);
else
messagedialog("INVALID");
}
}
异常堆栈跟踪:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Choose numbers 1-9"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Exer29A.main(Exer29A.java:50)
答案 0 :(得分:1)
与所有其他来电不同,对于案例',您拨打inputDialog(...)
而不是JOptionPane.showInputDialog(null, "Enter base.")
。这不是主要问题。
主要问题是 inputDialog(...)
返回的内容。这与JOptionPane.showInputDialog(null, "Enter base.")
返回的内容不同。