无法使用 JOptionPane 在 showConfirmDialog 中将 int 转换为 String
int cannot be converted to String in showConfirmDialog using JOptionPane
我正在尝试 运行 主 Java class 中的以下代码:
personName = JOptionPane.showInputDialog("Enter the name of the person !");
onepty.setNameOfPerson(personName);
SpeechDecision = JOptionPane.showConfirmDialog(null,
"Select Yes or No", "choose one", JOptionPane.YES_NO_OPTION);
onepty.speechCheck(speechDecision);
在数据定义 class 中定义了以下方法,我通过 onepty
对象访问该数据定义,如上所示:
public String speechCheck(String str){
if(str == "Yes" || str =="YES"||str == "Y" ||str== "y"||str=="YEs"||str=="yeS"||str=="yES"){
this.speechVar = str;
}
else {
this.speechVar = str;
}
}
但是在使用 jGrasp 编译后出现以下错误:
error: incompatible types: int cannot be converted to String
speechDecision = JOptionPane.showConfirmDialog(null,
^
1 error
虽然错误是不言自明的,但由于我对 jOptionPane 很陌生,所以我想知道用户在单击“是”或“否”选项后选择的按钮输入是否存储为整数而不是字符串?我需要修改我的 speechCheck
方法来捕获整数值吗?请指教。
变量 SpeechDecision
可能是字符串,它应该是一个 int
因为这是 showConfirmDialog
的返回值,看它的签名:
public static int showConfirmDialog(...)
↑
几点注意事项:
- 关注Java Naming Conventions
- 不要使用
==
比较字符串,而是使用 equals
- 为了更美好的世界缩进代码
你可以这样做:
int speechDecision = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
if (speechDecision == JOptionPane.YES_OPTION)
{
// Do something here
}
您必须使用 JOptionPane 默认常量
JOptionPane.YES_OPTION 是和
JOptionPane.NO_OPTION 否
并且 SpeechDecision 的类型应该是整数。
所以,代码看起来像,
int speechDecision = JOptionPane.showConfirmDialog(null, "Select Yes No", "choose one", JOptionPane.YES_NO_OPTION);
if (speechDecision == JOptionPane.YES_OPTION){
// Do something here for yes
}
else{
// Do something here for no
}
我正在尝试 运行 主 Java class 中的以下代码:
personName = JOptionPane.showInputDialog("Enter the name of the person !");
onepty.setNameOfPerson(personName);
SpeechDecision = JOptionPane.showConfirmDialog(null,
"Select Yes or No", "choose one", JOptionPane.YES_NO_OPTION);
onepty.speechCheck(speechDecision);
在数据定义 class 中定义了以下方法,我通过 onepty
对象访问该数据定义,如上所示:
public String speechCheck(String str){
if(str == "Yes" || str =="YES"||str == "Y" ||str== "y"||str=="YEs"||str=="yeS"||str=="yES"){
this.speechVar = str;
}
else {
this.speechVar = str;
}
}
但是在使用 jGrasp 编译后出现以下错误:
error: incompatible types: int cannot be converted to String
speechDecision = JOptionPane.showConfirmDialog(null,
^
1 error
虽然错误是不言自明的,但由于我对 jOptionPane 很陌生,所以我想知道用户在单击“是”或“否”选项后选择的按钮输入是否存储为整数而不是字符串?我需要修改我的 speechCheck
方法来捕获整数值吗?请指教。
变量 SpeechDecision
可能是字符串,它应该是一个 int
因为这是 showConfirmDialog
的返回值,看它的签名:
public static int showConfirmDialog(...)
↑
几点注意事项:
- 关注Java Naming Conventions
- 不要使用
==
比较字符串,而是使用equals
- 为了更美好的世界缩进代码
你可以这样做:
int speechDecision = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
if (speechDecision == JOptionPane.YES_OPTION)
{
// Do something here
}
您必须使用 JOptionPane 默认常量 JOptionPane.YES_OPTION 是和 JOptionPane.NO_OPTION 否 并且 SpeechDecision 的类型应该是整数。
所以,代码看起来像,
int speechDecision = JOptionPane.showConfirmDialog(null, "Select Yes No", "choose one", JOptionPane.YES_NO_OPTION);
if (speechDecision == JOptionPane.YES_OPTION){
// Do something here for yes
}
else{
// Do something here for no
}