我正在使用Eclipse和JDK 8创建一个用Java编写的程序。我使用以下代码创建了一个Scanner
:Scanner input = new Scanner(System.in);
然后,我使用代码创建了一个String
String command = "no";
我计划使用此字符串来控制代码中的while
循环,以及while
循环中的switch
。循环开头为: while(command = "no")
在while
循环中,我使用之前创建的Scanner
让用户定义String
和int
的值,这在执行代码时起作用。之后(但仍在while
循环中),我将 command = input.nextLine();
,后跟 input.close();
放在下面的行上,以设置command
字符串的值,并关闭Scanner
。但是当我执行代码时,它似乎跳过了它,以及我直接放在后面的switch
(它也应该由command
字符串控制),并在while
循环之后继续编码,即使command
仍然设置为"no",这应该使while
循环再次启动。
如何使代码暂停并等待在command = input.nextLine();
输入某些内容,以及如何使switch
正常工作?
以下是整个代码:
'import java.util.Scanner;
class Human {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
String name = this.name;
return name;
}
public int getAge() {
int age = this.age;
return age;
}
}
public class Asdfghjkl {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String command = "no";
int age;
String name;
while(command == "no") {
System.out.println("Welcome to HUMAN CREATOR");
System.out.println("What is the human's name?");
name = input.nextLine();
System.out.println("Okay, its name is " + name + ".");
System.out.println("What about its age?");
age = input.nextInt();
System.out.println(name + " is " + age + " years old.");
System.out.println("Is this correct?");
command = input.nextLine();
switch (command) {
case ("yes"):
System.out.println("Great! Lets make the human!");
Human h1 = new Human();
h1.setAge(age);
h1.setName(name);
case ("no"):
System.out.println("Okay, lets start again.");
}
}
System.out.println("qwerty");
input.close();
}
}
更改您的:
age = input.nextInt();
到一个:
age = Integer.parseInt(input.nextLine());
"nextInt()"不能正常工作的原因是因为它在队列中留下了一个空白空间,在下一个"nextLine()"中被挑选出来
此外,在比较两个字符串时,您应该始终使用 .equals("一些字符串")。
我希望这可以修复您的错误:)
此外,正如"充满鳗鱼的气垫船"指出的那样,如果您确实需要重复while循环,扫描仪将被关闭,因此会导致错误。
while(command.equals("no"))
而不是
while(command == "no")
而且case ("yes"):
最后没有break;
声明,所以它也将继续下一个案例。
Scanner#nextInt
方法不会读取输入的最后一个换行符 - 这里
while(command == "no") 也不会按预期工作,因为您测试字符串的相等性。执行此操作时,需要使用 .equals 来测试字符串值是否相等。
所以使用:
while(command.equals("no"))
在使用完之前,您似乎还在 while 循环内关闭了 Scanner 变量、输入。
while(!command.equalsIgnoreCase("yes")) {
System.out.println("Welcome to HUMAN CREATOR");
System.out.println("What is the human's name?");
name = input.nextLine();
System.out.println("Okay, its name is " + name + ".");
System.out.println("What about its age?");
age = input.nextInt();
System.out.println(name + " is " + age + " years old.");
System.out.println("Is this correct?");
command = input.nextLine();
input.close(); // !! ************ here *****
switch (command) {
case ("yes"):
System.out.println("Great! Lets make the human!");
Human h1 = new Human();
h1.setAge(age);
h1.setName(name);
case ("no"):
System.out.println("Okay, lets start again.");
}
}
在 while 循环之后关闭它,以便它在循环的每次迭代中仍然可用,这不是更有意义吗?
此外,在获取捕获行尾令牌input.nextInt()
后,还可以添加一个额外的input.nextLine();
:
age = input.nextInt();
input.nextLine();
试试这个: while(command.equals("no"))