Java do中的Scanner.nextLine(),while循环。退一步
我有两个问题。 1.若用户并没有在控制台中输入正确的命令,那个么在“do,while loop”中,我很难找到一种退一步的方法。 2.这样做之后,我想在switch循环中使用用户输入,这样就可以处理所选的命令,但我不确定将什么作为变量放入括号中进行测试,因为它无法识别用户输入 以下是我迄今为止所做的工作。我尝试过很多事情,但没有一件成功Java do中的Scanner.nextLine(),while循环。退一步,java,Java,我有两个问题。 1.若用户并没有在控制台中输入正确的命令,那个么在“do,while loop”中,我很难找到一种退一步的方法。 2.这样做之后,我想在switch循环中使用用户输入,这样就可以处理所选的命令,但我不确定将什么作为变量放入括号中进行测试,因为它无法识别用户输入 以下是我迄今为止所做的工作。我尝试过很多事情,但没有一件成功 `package assignment6; public class Enumeracija { public enum Commands {
`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;*/
`包分配6;
公共类Enumeracija{
公共枚举命令{
属性,新建文件夹,重命名,复制,剪切,删除,未指定
}}
一揽子转让6;
导入assignment6.Enumeracija.Commands;
导入java.io.*;
导入java.util.Scanner;
公共课堂作业6{
公共静态布尔主(字符串[]args)抛出FileNotFoundException,
IOException{
//向用户显示命令
对于(inti=0;i首先,Java中main方法的返回类型应该始终为void
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语句,因为命令是枚举形式的,所以需要正确地引用它们
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;
让我知道这是否有效。您正在通过返回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语句的问题是,您将字符串传递给switch,而案例是枚举。
将字符串转换为枚举对象
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...");
}
您正在使用userInput,它是switch语句中的字符串对象。有关说明,请参阅
此外,主要类别的签名为:
public static void (String[] whateverHere)
但我发现主类未找到的问题-这就是你的问题?好吧,所以2.问题很容易解决。我意识到我不能将字符串放入开关,除非事先将其转换为枚举,反之亦然,这取决于我要“检查”的内容。但1.问题仍然是开放的。我使用.equals得到的结果也不能那么简单,因为我想将字符串与枚举进行比较。因为我使用FOR循环,但在这之后,while循环的所有部分都无法完成,我试图找出如何将FOR循环放在while循环中,因此程序不会停止运行,直到命令编写不正确为止。`do{String input=userInput.nextLine();FOR(Commands c:Commands.values()){if(input.toUpperCase().trim().equals(c.toString()){System.out.println(“您选择了“+input.trim().toUpperCase()”+“command”);correctCommand=true;}否则{correctCommand=false;System.out.println(“输入错误。继续尝试…”)谢谢,我在第二部分意识到了我的错误,但第一部分仍然让我困惑。`do{String input=userInput.nextLine();for(Commands c:Commands.values()){if(input.toUpperCase().trim().equals(c.toString()){System.out.println(“您选择了”+input.trim().toUpperCase()+“command.”;correctCommand=true;}else{correctCommand=false;System.out.println(“输入错误。继续尝试…”);}}}}}}}}}while(correctCommand);`我需要修改那个部分,因为在FOR循环中使用.equals显然只有这样才可能(我正在比较输入(字符串)和枚举)。现在我被卡住了。While循环没有做我需要做的事情,这是在用户没有正确编写命令之前不会停止程序。第一部分基本上是这样的,但我需要更改代码,因为.equals方法:`do{String input=userInput.nextLine();for(Commands c:Commands.values()){if(input.toUpperCase().trim().equals(c.toString()){System.out.println(“您选择了“+input.trim().toUpperCase()”+“command”);correctCommand=true;}否则{correctCommand=false;System.out.println(“输入错误。继续尝试…”)}}while(correctCommand);`当我将字符串转换为Enum时,同样的problemSwitch部分仍然有效。
public static void (String[] whateverHere)