如何解决如何在扫描仪中设置无限循环?
我是Java的初学者,所以对此有一个疑问 我正在尝试创建一个简单的代码来完成它,只是我想知道您如何设置一个用户可以输入的无限提问的问题?我现在拥有的那个执行无限循环,这没有帮助...
import java.util.Scanner;
public class Logical_Operators {
public static void main(String[] args) {
// Todo Auto-generated method stub
Scanner input = new Scanner(system.in);
//this is a conditional scenario,where only boys above the age of 18 can enter
//and only girls equal to or over the age of 20
String gender,boy,girl,response;
int age;
System.out.println("Welcome to the party! Would you like to enter?");
response = input.nextLine();
while(!response.equals("yes") && !response.equals("no")) {
System.out.println("Please say Yes or No");
}
{
if(response.equals("yes")) {
System.out.println("What gender are you? Type boy or girl.");
gender = input.nextLine();
System.out.println("What about your age?");
age = input.nextInt();
if(gender.equals("boy") && age >= 18) {
System.out.println("You can enter,Welcome!");
}
else if(gender.equals("girl") && age >= 20) {
System.out.println("You can enter,Welcome!"); }
else { System.out.println("Sorry,you may not enter because you don't meet the age requirements"); }
}
else if(!(response.equals("yes")||response.equals("no"))) {
System.out.println("Please say Yes or No");
}
else {
System.out.println("Bye,hope to see you again!");
}
System.exit(1);
}
}
}
解决方法
为了摆脱循环,您需要获取一个新值进行测试
response = input.nextLine();
while(!response.equals("yes") && !response.equals("no")) {
System.out.println("Please say Yes or No");
response = input.nextLine();
}
,
- 您需要将逻辑放置在代码中当前不存在的循环中。在您的代码中,循环中只有
System.out.println("Please say Yes or No");
。 - 您可以使用无限循环(即
while (true) { }
),当输入与退出条件匹配时,您可以break
。 - 此外,我建议您使用
equalsIgnoreCase
而不是equals
来以不区分大小写的方式测试相等性。 - 我还建议您使用
age = Integer.parseInt(input.nextLine())
而不是age = input.nextInt()
来避免讨论的问题here。
演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// this is a conditional scenario,where only boys above the age of 18 can enter
// and only girls equal to or over the age of 20
String gender,response;
int age;
while (true) {
System.out.println("Welcome to the party! Would you like to enter?");
System.out.print("Please say Yes or No: ");
response = input.nextLine();
if (response.equalsIgnoreCase("no")) {
System.out.println("Bye,hope to see you again!");
break;
}
if (response.equalsIgnoreCase("yes")) {
System.out.print("What gender are you? Type boy or girl: ");
gender = input.nextLine();
System.out.print("What about your age?: ");
age = Integer.parseInt(input.nextLine());
if (gender.equalsIgnoreCase("boy") && age >= 18) {
System.out.println("You can enter,Welcome!");
} else if (gender.equalsIgnoreCase("girl") && age >= 20) {
System.out.println("You can enter,Welcome!");
} else {
System.out.println("Sorry,you may not enter because you don't meet the age requirements");
}
}
}
}
}
示例运行:
Welcome to the party! Would you like to enter?
Please say Yes or No: yes
What gender are you? Type boy or girl: boy
What about your age?: 24
You can enter,Welcome!
Welcome to the party! Would you like to enter?
Please say Yes or No: yes
What gender are you? Type boy or girl: girl
What about your age?: 19
Sorry,you may not enter because you don't meet the age requirements
Welcome to the party! Would you like to enter?
Please say Yes or No: yes
What gender are you? Type boy or girl: girl
What about your age?: 21
You can enter,Welcome!
Welcome to the party! Would you like to enter?
Please say Yes or No: no
Bye,hope to see you again!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。