所以我环顾四周并看到了一些类似的问题,但我仍然无法让我的程序运行。我只是在家里练习(我在高中)不能让这个问题悬而未决并继续前进。这是我的代码,但我不确定我做错了什么。
String inputAge, outputOK, outputCancel;
Integer Age;
inputAge = JOptionPane.showInputDialog("Enter Age To Find Your Year Of Birth", JOptionPane.OK_CANCEL_OPTION);
if (inputAge == JOptionPane.OK_OPTION){
System.out.println("You Were Born In The Year " + (2018 - (Age = Integer.parseInt(inputAge))));
} else if (inputAge == JOptionPane.CANCEL_OPTION){
System.exit(1);
}
最佳答案
that the first type: java.lang.String and the second type: int.
showInputDialog(...)
方法返回一个 String,而不是一个 int。所以你不能只将值分配给 int。您需要将 String 转换为 int。像这样的东西:
String value = JOptionPane.showInputDialog(...);
int age = Integer.parseInt(value);
关于java - 如何向 JOptionPane.showInputDialog 上的按钮添加命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49469450/