非法线以字符字面结尾
**关于代码:我只是使用开关语句制作一个简单的代码。所有开关案例都可以正常工作,除了两位数的案例。我有一个错误说:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的变量
年
是一个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.首先,在
案例'12'
中存在语法错误,应该是案例'12':
(也很糟糕。缩进不良使得很难调试)
我建议您将此代码转换为基于
String
基于一个。请检查下面的完整示例。char只能获得一个字符,
char年;
引起错误,因为在10、11、12之类的输入值中它无法正常工作First of all, there is a Syntax error in
case '12'
it should becase '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