简体   繁体   中英

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! in this case that i'm posting it to you, the condition in the do-while loop doesn't work good. 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. Use the && operator

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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