非常感谢您在广告方面的帮助。!
使用枚举类型的扫描仪时出错。然而,我不允许在这个任务中使用Buffer(InputStreamReader)。围绕它最好的工作是什么?
我收到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted
at ui.Application.runCLI(Application.java:33)
at ui.Application.main(Application.java:13)
代码:
package ui;
import java.util.Scanner;
公开课申请{
static String command;
public enum Command {
CONNECT, DISCONNECT, SEND, LOGLEVEL, HELP, QUIT, EXIT
}
private static void run(Scanner sc) {
// String command; // ready for the input
boolean done = false; // ready for the menu loop
while (!done) { // keep on until done
System.out
.println("Milestone1: Connection and interation with TCP server");
System.out
.println("-------------------Please select on of the commandso-------------------------------------");
System.out.println("connect");
System.out.println("disconnect");
System.out.println("send");
System.out.println("logLevel");
System.out.println("help");
System.out.println("quit");
System.out.println("exit");
command = sc.nextLine(); // take user input
Command cmd=null;
try{
cmd=Command.valueOf(command.toUpperCase());
}
catch (IllegalArgumentException e){
System.out.println("Invalid input");
return;
}
switch (cmd) {
case EXIT: // exit menu
done = true;// condition for breaking the loop
break;
case CONNECT:
System.out.print(" IP adress: ");
try {
String userInput = sc.toString(); // user Input
System.out.println(" Port: ");
int userInput1 = sc.nextInt();// user Input
if (userInput1 >= 0) {
System.out.println(" EcoClient>" + " " + command + " "
+ userInput + " " + userInput1);
} else {
System.out
.println("Entered value for Port is negative number or IP adress length < 7 || > 15, not in n.n.n.n format ");
}
}
catch (Exception e) {// throw exception in case of illogical
// input
System.out.println("\nBad input, please try again ");
sc.nextLine(); // remove leftover "\n"
}
break;
case DISCONNECT:
System.out.println(" EcoClient>" + " " + command);
break;
case SEND:
System.out
.println("Please enter " + " Hello World " + "phrase");
try {
String userInput = sc.toString(); // user Input
System.out.println(" EcoClient>" + " " + command + " "
+ userInput);
}
catch (Exception e) {// throw exception in case of illogical
// input
System.out.println("\nBad input, please try again ");
sc.nextLine(); // remove leftover "\n"
}
break;
case LOGLEVEL:
try {
System.out.println(" EcoClient>" + " " + command + "< "
+ "current log status" + " >");
}
catch (Exception e) {// throw exception in case of illogical
// input
System.out.println("\nBad input, please try again ");
sc.nextLine(); // remove leftover "\n"
}
break;
case HELP:
try {
System.out
.println("Following set of commands provide following functionalities:"
+ " connect: establishes connection to the eco server "
+ "disconnect: disconnects from the server and receives confirmation message "
+ "send: sends the message to the server "
+ "logLevel: prints out current log status"
+ "quit: quits and notifies user about program shut down "
+ "exit: cancel the input");
}
catch (Exception e) {// throw exception in case of illogical
// input
System.out.println("\nBad input, please try again ");
sc.nextLine(); // remove leftover "\n"
}
break;
case QUIT:
try {
System.out.println(" EcoClient> " + command);
}
catch (Exception e) {// throw exception in case of illogical
// input
System.out.println("\nBad input, please try again ");
sc.nextLine(); // remove leftover "\n"
}
break;
default:
System.out.println("Does not recognise "
+ "the input, pl. try again");
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);// will take user input
run(sc);
}
}
<代码>sc。nextLine()返回一个String。您需要使用静态方法Command将其转换为
,它解析字符串并返回匹配的Command
的实例(您正在其中使用0.05开关
>)。valueOf(String)Command
instance。
以下是基础知识:
command = sc.nextLine(); // take user input
Command cmd = null;
try {
cmd = Command.valueOf(command.toUpperCase());
} catch (IllegalArgumentException e) {
System.out.println("Invalid input, sorry."); //This is given on invalid input. Put whatever type of error message you want here.
return;
}
switch (cmd) {
//...
问题内容: 例如,我该怎么做: 结果示例: 问题答案: 迅捷4.2+ 从Swift 4.2(使用Xcode 10)开始,只需添加协议一致性即可从中受益。要添加此协议一致性,您只需要在某处写: 如果枚举是您自己的,则可以直接在声明中指定一致性: 然后,以下代码将打印所有可能的值: 与早期Swift版本(3.x和4.x)的兼容性 如果您需要支持Swift 3.x或4.0,则可以通过添加以下代码来模仿S
问题内容: 我有一个电影租借系统的现有数据库。每部电影都有一个评级属性。在SQL中,他们使用约束来限制此属性的允许值。 我认为使用Java枚举将约束映射到对象世界会很好。但是由于“ PG-13”和“ NC-17”中的特殊字符,不可能简单地获取允许的值。因此,我实现了以下枚举: 使用toString()方法,方向enum-> String可以正常工作,但是String-> enum不能正常工作。我得
rank ▲ ✰ vote url 7 1112 431 1201 url 在Python里如何用枚举类型? 我是一个C#开发者,但是我现在做的工作是关于Python的. 怎么在Python里代替枚举类型呢? PEP435标准里已经把枚举添加到Python3.4版本,在Pypi中也可以向后支持3.3, 3.2, 3.1, 2.7, 2.6, 2.5, 和 2.4版本. 如果想向后兼容$ pip i
当我们需要定义常量时,一个办法是用大写变量通过整数来定义,例如月份: JAN = 1 FEB = 2 MAR = 3 ... NOV = 11 DEC = 12 好处是简单,缺点是类型是int,并且仍然是变量。 更好的方法是为这样的枚举类型定义一个class类型,然后,每个常量都是class的一个唯一实例。Python提供了Enum类来实现这个功能: from enum import Enum
例如,我如何做类似的事情: 结果示例:
Go语言中是否有枚举的简单实现?像下面这样的东西?