【发布时间】:2021-06-14 23:36:53
【问题描述】:
我试图从用户那里获取一个整数(年龄)的输入。但我不断收到“字符串无法转换为字符串”和“二进制运算符'='的错误操作数类型。任何帮助将不胜感激
import javax.swing.JOptionPane;
class LotionFinder {
public static void main(String[] args) {
String response;
response = Integer.parseInt(JOptionPane.showInputDialog ("Please Enter Your Age:"));
if (response == null) {
JOptionPane.showMessageDialog(null, "Please Contact a Sales Representative For a Private Consultation");
} else if (response.equals("")) {
JOptionPane.showMessageDialog(null, "Please Contact a Sales Representative For a Private Consultation");
} else if (response <= 3 && response >= 1){
JOptionPane.showMessageDialog(null, "It's Recommended That You Purchase Our No-Tears Baby Lotion!");
}
System.exit (0);
}
}
【问题讨论】:
-
将
String response更改为Integer response -
非常感谢,我花了 2 个小时才弄清楚这一点,哈哈
-
如果用户没有输入任何值,您的程序将无法运行。
-
嗯,好的 nvm,我该如何解决这个问题?
-
在您的问题上添加错误的堆栈跟踪总是好的。
标签: java string compiler-errors integer