输入错误不能解决为线程main.lang中的变量和异常

发布于 2025-02-05 03:21:03 字数 1120 浏览 0 评论 0原文

以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹短裙飘 2025-02-12 03:21:03

首先,由于某种原因,您的代码中有两个浮动'字符。您应该将它们删除,因为它们会导致错误;我假设这是传输代码堆叠溢出时犯的错误。

发生错误是因为您尚未定义输入的类型。 joptionpane.showinputdialog()的返回值应为string,所以更改

input = JOptionPane.showInputDialog("Enter a, b or c:");

String input = JOptionPane.showInputDialog("Enter a, b or c:");

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 for JOptionPane.showInputDialog() should be String, so change:

input = JOptionPane.showInputDialog("Enter a, b or c:");

to

String input = JOptionPane.showInputDialog("Enter a, b or c:");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文