微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在扫描仪中设置无限循环?

我是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();
}
,
  1. 您需要将逻辑放置在代码中当前不存在的循环中。在您的代码中,循环中只有System.out.println("Please say Yes or No");
  2. 您可以使用无限循环(即while (true) { }),当输入与退出条件匹配时,您可以break
  3. 此外,我建议您使用equalsIgnoreCase而不是equals来以不区分大小写的方式测试相等性。
  4. 我还建议您使用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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?