非法线以字符字面结尾

发布于 2025-01-19 09:58:54 字数 1740 浏览 0 评论 0原文

**关于代码:我只是使用开关语句制作一个简单的代码。所有开关案例都可以正常工作,除了两位数的案例。我有一个错误说:

ear.java:37:错误:未锁定字符字面 案例'10'
Year.java:40:错误:未锁定字符字面 案例'11':year.java:43:错误:未闭合字符字面 案例'12'

代码:

import java.util.Scanner;

public class year {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        char year;
        System.out.println("Enter the number of the month ");
        year = input.next().charAt(0);
        switch(year){
            case '1': 
            System.out.println("January");
            break;
            case '2':
            System.out.println("Febraury");
            break;
            case '3':
            System.out.println("March");
            break;
            case '4':
            System.out.println("April");
            break;
            case '5':
            System.out.println("May");
            break;
            case '6':
            System.out.println("June ");
            break;
            case '7':
            System.out.println("July");
            break;
            case '8':
            System.out.println("August ");
            break;
            case '9':
            System.out.println("September ");
            break;
            case '10':
            System.out.println("October");
            break;
            case '11':
            System.out.println("November");
            break;
            case '12'
            System.out.println("December");
            break;
            default:
            System.out.println("Invalid");

        }
        input.close();

    }
}

我尝试在这里和那里进行一些更改,但无法理解它们,因此无法做到。

**About the code: I am just making a simple code using a switch statement. All the switch cases work fine except the double-digit cases. I get an error saying :

year.java:37: error: unclosed character literal
case '10'
year.java:40: error: unclosed character literal
case '11':year.java:43: error: unclosed character literal
case '12'

Code :

import java.util.Scanner;

public class year {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        char year;
        System.out.println("Enter the number of the month ");
        year = input.next().charAt(0);
        switch(year){
            case '1': 
            System.out.println("January");
            break;
            case '2':
            System.out.println("Febraury");
            break;
            case '3':
            System.out.println("March");
            break;
            case '4':
            System.out.println("April");
            break;
            case '5':
            System.out.println("May");
            break;
            case '6':
            System.out.println("June ");
            break;
            case '7':
            System.out.println("July");
            break;
            case '8':
            System.out.println("August ");
            break;
            case '9':
            System.out.println("September ");
            break;
            case '10':
            System.out.println("October");
            break;
            case '11':
            System.out.println("November");
            break;
            case '12'
            System.out.println("December");
            break;
            default:
            System.out.println("Invalid");

        }
        input.close();

    }
}

I tried doing a few changes here and there but couldn't understand them and thus could not do so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

余罪 2025-01-26 09:58:54

您的变量是一个char。炭只能是一个字符。

因此,当您尝试做'11''12'时,您会遇到这些“ chars”,因为这些“ chars”由多个字符组成。

这里的快速解决方案是使用input.next()不使用.charat(0)使用input.next()。然后,您需要更改案例语句以使用双引号而不是单引号。

另外,您可以执行integer.parseint(input.next()),然后像@Tom所建议的那样打开int。

Your variable year is a char. A char can only be a single character.

Therefore when you try and do '11' or '12' you run into issues as these "chars" consist of more than one character.

The quick solution here would be to use a String instead of char, using input.next() without the .charAt(0). Then you would need to change your case statements to use double quotes instead of single quotes.

Alternatively, you could do Integer.parseInt(input.next()) and then switch on an int instead, as @Tom has suggested.

海夕 2025-01-26 09:58:54

首先,在案例'12'中存在语法错误,应该是案例'12':
(也很糟糕。缩进不良使得很难调试)

我建议您将此代码转换为基于String基于一个。请检查下面的完整示例。
char只能获得一个字符,char年;引起错误,因为在10、11、12之类的输入值中它无法正常工作

import java.util.Scanner;

public class year {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String year;
        System.out.println("Enter the number of the month ");
        year = input.nextLine();
        switch(year){
            case "1": 
            System.out.println("January");
            break;
            case "2":
            System.out.println("Febraury");
            break;
            case "3":
            System.out.println("March");
            break;
            case "4":
            System.out.println("April");
            break;
            case "5":
            System.out.println("May");
            break;
            case "6":
            System.out.println("June ");
            break;
            case "7":
            System.out.println("July");
            break;
            case "8":
            System.out.println("August ");
            break;
            case "9":
            System.out.println("September ");
            break;
            case "10":
            System.out.println("October");
            break;
            case "11":
            System.out.println("November");
            break;
            case "12":
            System.out.println("December");
            break;
            default:
            System.out.println("Invalid");

        }
        input.close();
    }
}

First of all, there is a Syntax error in case '12' it should be case '12':
(Also the code indention is bad. Poor indentation make it hard for debugging)

I suggest you convert this code to a String based one. Please check the complete example below.
Char can get one character only and char year; cause a bug, because in input values like 10, 11, 12 it won't work as expected

import java.util.Scanner;

public class year {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String year;
        System.out.println("Enter the number of the month ");
        year = input.nextLine();
        switch(year){
            case "1": 
            System.out.println("January");
            break;
            case "2":
            System.out.println("Febraury");
            break;
            case "3":
            System.out.println("March");
            break;
            case "4":
            System.out.println("April");
            break;
            case "5":
            System.out.println("May");
            break;
            case "6":
            System.out.println("June ");
            break;
            case "7":
            System.out.println("July");
            break;
            case "8":
            System.out.println("August ");
            break;
            case "9":
            System.out.println("September ");
            break;
            case "10":
            System.out.println("October");
            break;
            case "11":
            System.out.println("November");
            break;
            case "12":
            System.out.println("December");
            break;
            default:
            System.out.println("Invalid");

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