简体   繁体   English

同时执行条件(OR)不起作用

[英]do-while condition(OR) doesn't work

this is a simple program that would get first and last name from user and print them with a space between them,the loop must break if user inputs "END" instead any of the first or last name! 这是一个简单的程序,可以从用户那里获得名字和姓氏,并在它们之间打印一个空格。如果用户输入“ END”而不是名字或姓氏,则循环必须中断! in this case that i'm posting it to you, the condition in the do-while loop doesn't work good. 在这种情况下,我将其发布给您,do-while循环中的条件无法正常工作。 now what's the problem??? 现在出了什么问题???

import java.util.Scanner;

public class EndExit {
    public static void main(String[] args){


        Scanner in = new Scanner(System.in);

        String name1;
        String name2;
        String end = "END" ;


        do{
            System.out.println("Enter First Name:");
            name1 = in.next();
            System.out.println("Enter Last Name:");
            name2 = in.next();

            System.out.println(name1 + " " +name2);

        }while( !(name1.equalsIgnoreCase(end)) || !(name2.equalsIgnoreCase(end)) );

        System.out.println("You are out!");
    }
}

One while condition will always be false causing the loop to continue. while条件将始终为假,从而导致循环继续。 Use the && operator 使用&&运算符

} while( !(name1.equalsIgnoreCase(end)) && !(name2.equalsIgnoreCase(end)) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM