由于错误异常处理,我提前退出循环,因为我使用以下行编辑了代码:
try {
cmd = Command.valueOf(command.toUpperCase());
} catch (IllegalArgumentException e) {
System.out.println("Invalid input");
return;
}
只想退出EXIT案例
CODE:
package ui;
import java.util.Scanner;
public class Application {
static String command;
public enum Command {
CONNECT, DISCONNECT, SEND, LOGLEVEL, HELP, QUIT, EXIT
}
private static void run(Scanner sc) {
System.out.println("Milestone1: Connection and interation with TCP server");
// String command; // ready for the input
boolean done = false; // ready for the menu loop
while (!done) { // keep on until done
System.out.println("\n-------------------Please select one of the commands-------------------------------------");
System.out.println("\nCONNECT, DISCONNECT, SEND, LOGLEVEL, HELP, QUIT, 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.println(" IP adress: ");
String userInput = sc.next(); // user Input
try {
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 ");
try {
String userInput2 = sc.next(); // user Input
System.out.println(" EcoClient>" + " " + command + " "
+ userInput2);
} 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);
}
}
答案 0 :(得分:4)
看起来您想使用continue
(开始下一个循环迭代)而不是return
。
答案 1 :(得分:0)
public enum Command {
CONNECT, DISCONNECT, SEND, LOGLEVEL, HELP, QUIT, EXIT, XX
}
try {
cmd = Command.valueOf(command.toUpperCase());
} catch (IllegalArgumentException e) {
System.out.println("Invalid input");
cmd = XX;
}