【发布时间】:2024-01-03 17:21:01
【问题描述】:
我是 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);
}
}
}
【问题讨论】:
-
请修正你的源代码的缩进,这样更容易阅读(特别适合你)。
标签: java while-loop equals control-structure