如何在扫描器中设置无限循环?

huangapple go评论72阅读模式
英文:

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();
}

huangapple
  • 本文由 发表于 2020年10月25日 05:20:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64518114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定