输入错误不能解决为线程main.lang中的变量和异常
以下代码:
import javax.swing.JOptionPane;
public class TheSwitchState{
public static void main(String[] args) {
' char letter;
input= JOptionPane.showInputDialog("Enter a, b or c:");
letter= input.charAt(0); '
switch(letter)
{
case 'a':
JOptionPane.showMessageDialog(null,"please enter a :");
break;
case 'b':
JOptionPane.showMessageDialog(null,"please enter b :");
break;
case 'c':
JOptionPane.showMessageDialog(null,"please enter c :");
break;
default:
JOptionPane.showMessageDialog(null,"Enter characater is invalid !!");
}
System.exit(0);
}
}
给出以下错误:
<Exception in thread "main" java.lang.Error: Unresolved compilation problems:
input cannot be resolved to a variable
input cannot be resolved
at TheSwitchState.main(SwitchState.java:8)>
有人可以解释为什么会发生此错误,并提供有关如何修复它的一些建议?
The following code:
import javax.swing.JOptionPane;
public class TheSwitchState{
public static void main(String[] args) {
' char letter;
input= JOptionPane.showInputDialog("Enter a, b or c:");
letter= input.charAt(0); '
switch(letter)
{
case 'a':
JOptionPane.showMessageDialog(null,"please enter a :");
break;
case 'b':
JOptionPane.showMessageDialog(null,"please enter b :");
break;
case 'c':
JOptionPane.showMessageDialog(null,"please enter c :");
break;
default:
JOptionPane.showMessageDialog(null,"Enter characater is invalid !!");
}
System.exit(0);
}
}
Gives the following error:
<Exception in thread "main" java.lang.Error: Unresolved compilation problems:
input cannot be resolved to a variable
input cannot be resolved
at TheSwitchState.main(SwitchState.java:8)>
Could someone please explain why this error is happening, as well as provide some suggestions as to how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,由于某种原因,您的代码中有两个浮动
'
字符。您应该将它们删除,因为它们会导致错误;我假设这是传输代码堆叠溢出时犯的错误。发生错误是因为您尚未定义
输入
的类型。joptionpane.showinputdialog()
的返回值应为string
,所以更改:
First of all, there are two floating
'
characters in your code, for some reason. You should remove them, as they are causing errors; I am assuming it was a mistake made when transferring over the code to Stack Overflow.The error is happening because you have not defined a type for
input
. The return value forJOptionPane.showInputDialog()
should beString
, so change:to