Java 如何在扫描仪中设置无限循环?
我是Java的初学者,所以我有一个问题 我正在尝试创建一个简单的代码,我已经完成了,我只是想知道如何设置一个用户可以输入的无限提问问题?我现在有一个无限循环,这是没有帮助的Java 如何在扫描仪中设置无限循环?,java,while-loop,equals,control-structure,Java,While Loop,Equals,Control Structure,我是Java的初学者,所以我有一个问题 我正在尝试创建一个简单的代码,我已经完成了,我只是想知道如何设置一个用户可以输入的无限提问问题?我现在有一个无限循环,这是没有帮助的 import java.util.Scanner; public class Logical_Operators { public static void main(String[] args) { // TODO Auto-generated method
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(“请说是或否”)代码>在循环中
while(true){}
),当输入与退出条件匹配时,您可以中断该循环
equalsIgnoreCase
而不是equals
以不区分大小写的方式测试相等性age=Integer.parseInt(input.nextLine())
而不是age=input.nextInt()
,以避免讨论的问题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!
运行示例:
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!
请修正你源代码的缩进,这使它更容易阅读(特别是对你来说)。嗨,谢谢你的回复,它成功了。但是有没有一种方法可以让我对性别和年龄做同样的事情呢?就像无限循环一样。我尝试过使用相同的方法,但即使我输入男孩或女孩,它也会继续问这个问题。你应该能够使用完全相同的逻辑。记住用你要求的新术语替换“是”和“否”