英文:
How do you set up an infinite loop in scanners?
问题
import java.util.Scanner;
public class Logical_Operators {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String gender, 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");
response = input.nextLine();
}
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);
}
}
英文:
I'm a beginner in Java, so I had a question on this
I'm trying to create a simple code in which I'm done with it, just I wanted to know how do you set up an infinitely asking question that the user can input? The one I have right now does an infinite loop, which is not helpful...
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);
}
}
}
答案1
得分: 1
为了跳出循环,您需要获取一个新值进行测试
response = input.nextLine();
while (!response.equals("yes") && !response.equals("no")) {
System.out.println("请回答是或否");
response = input.nextLine();
}
英文:
In order to get out of the loop, you need to get a new value to test
response = input.nextLine();
while(!response.equals("yes") && !response.equals("no")) {
System.out.println("Please say Yes or No");
response = input.nextLine();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论