我有2个问题。 1.如果用户没有在控制台中输入正确的命令,我有一个问题就是在'do,while循环'中找到退步的方法。 2.执行此操作后,我想在开关循环中使用用户输入,因此可以处理所选命令,但我不确定将哪些内容放入括号中作为要测试的变量,因为它无法识别用户输入。
这是我到目前为止所做的。我已经尝试了很多东西,但没有尝试过。
`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;*/
答案 0 :(得分:0)
首先,Java中main方法的返回类型应该总是无效。
public static void main(String[] args) throws FileNotFoundException, IOException
其次,如果用户输入了正确的命令,程序将在您使用return语句时结束。如果你使用一个布尔变量作为标志,将其值设置为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语句,因为命令是enums的形式,你需要正确引用它们。
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;
让我知道这是否有效。
答案 1 :(得分:0)
您正在通过返回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
switch语句的问题在于您要将String传递给switch,而案例是Enums。 将String转换为Enum对象
Commands value = Commands.valueOf(userInput);
switch(value){
//processing
}
答案 2 :(得分:0)
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
答案 3 :(得分:0)
您可以使用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...");
}
您正在使用userInput,它是switch语句中的String
对象。 Click here有关此的解释。
主要类的签名是:
public static void (String[] whateverHere)