我有2个问题。 1.如果用户不输入正确的命令,我有问题要找到一种方法"做,循环"。 2.这样做之后,我想在开关循环中使用用户输入,因此可以处理所选的命令,但是我不确定该将什么放入括号中作为测试变量,因为它无法识别用户输入。p>这是我到目前为止所做的。我已经尝试了很多事情,但是它们没有解决。
`package assignment6;
public class Enumeracija {
public enum Commands {
PROPERTIES, NEW_FOLDER, RENAME, COPY, CUT, DELETE, UNSPECIFIED
}}
package assignment6;
import assignment6.Enumeracija.Commands;
import java.io.*;
import java.util.Scanner;
public class Assignment6 {
public static boolean main(String[] args) throws FileNotFoundException,
IOException {
//Showing commands to user
for (int i = 0; i<Commands.values().length-1; i++){
System.out.println(Commands.values()[i]);
}
//Reading users input
Scanner scan = new Scanner(System.in);
System.out.println("Enter one of the given commands: ");
String userInput;
/* WHILE DO LOOP so user can make a mistake and program will not stop
running, until he writes it correctly. But Im getting main class not
found problem, when I want to run it.*/
do {
userInput = scan.nextLine().trim().toUpperCase();
if (userInput.equals(Commands.values())) {
return true;}
else {
System.out.println("Input error. Keep trying and make sure you write command correctly.");
return false;}
} while (false);
switch(userInput) {
/*If I use userInput, it does not recognise commands
that user enters. Every case is being unrecognized.*/
case PROPERTIES:
//;
break;
case NEW_FOLDER:
//;
break;
case RENAME:
//;
break;
case COPY:
//;
break;
case CUT:
//;
break;
case DELETE:
//;
break;
case UNSPECIFIED:
//;
break;*/
首先,java中主方法的返回类型应始终无效。
public static void main(String[] args) throws FileNotFoundException, IOException
第二,如果用户输入正确的命令,则程序将在您使用返回语句时结束。如果您将布尔变量用作标志,最好将其值设置为true,然后突破循环。
boolean flag = false;
do {
userInput = scan.nextLine().trim().toUpperCase();
if (userInput.equals(Commands.values())) {
flag = true;
break;}
else {
System.out.println("Input error. Keep trying and make sure you write command correctly.");
}
} while (flag);
这将使用户保持在循环中,直到输入正确的命令为止。
现在,来到Switch语句,由于命令是枚举的形式,您需要正确参考它们。
switch(userInput) {
case Commands.PROPERTIES:
//;
break;
case Commands.NEW_FOLDER:
//;
break;
case Commands.RENAME:
//;
break;
case Commands.COPY:
//;
break;
case Commands.CUT:
//;
break;
case Commands.DELETE:
//;
break;
case Commands.UNSPECIFIED:
//;
break;
让我知道这是否有效。
do {
userInput = scan.nextLine().trim().toUpperCase();
// telling you what to do here will take the entire fun out of the problem
// HINT: check what Command.values() actually does and also what userInput.equals() actually does.
// HINT: once you have figured out how to interpret the user input, you need to store it in a variable to be used in the switch statement
// HINT: the variable that you store it in should be of the Command type (this should be more than sufficient a hint)
if (userInput.equals(Commands.values())) {
// do not return here. Return will cause the execution of the main method to stop
// what you actually want over here is break;
// this will allow you to break out of the infinite loop once your conditions are met.
}
else {
System.out.println("Input error. Keep trying and make sure you write command correctly.");
// remove the return condition here. This will stop execution of the main method
}
} while (true); // change condition to true so that loop is infinite
您正在通过返回true/false停止程序流。而是保持变量以保持跟踪
do {
boolean validInput = false; // this will keep track
userInput = scan.nextLine().trim().toUpperCase();
if (userInput.equals(Commands.values())) {
validInput = true; // make flag as true on valid input
}
else {
System.out.println("Input error. Keep trying and make sure you write command correctly.");
validInput = false; // make flag as false on invalid input
}
} while (!validInput); //repeat the loop if invalid input
开关语句的问题是,您将字符串传递给切换,而案例是枚举。将字符串转换为枚举对象
Commands value = Commands.valueOf(userInput);
switch(value){
//processing
}
-
您可以使用
while
循环,而不是do while
循环。这使代码更可读,因此更易于调试。boolean flag = true; while(flag){ userInput = scan.nextLine().trim().toUpperCase(); if (userInput.equals(Commands.values())) flag = false; else System.out.println("Input error..."); }
-
您使用的是Switch语句中的
String
对象的UserInput。单击此处以对此进行说明。
主类的签名也是:
public static void (String[] whateverHere)