简体   繁体   中英

How to get user input into a for loop and make the program loop until user enters quit

How do I get user input into a for loop and make the program loop until user enters quit?

Here is my code:

System.out.println("what is your full name?" );
Name = scan.nextLine();
String newString = Name.replaceAll(" ", "");
System.out.println("please enter " + newString.length() + " numbers and the total will be calculated");

if (newString.length() == 3) {
    for(int count = 1; count <= newString.length(); count++)
        System.out.println( "Number " + count + ":" + );
    Num1 = scan.nextInt();
    Num2 = scan.nextInt();
    Num3 = scan.nextInt();
    System.out.println("total is " + (Num1 + Num2 + Num3));
}

Inside the for loop try this:

    boolean flag = true;
    while(flag) {
        String input = scan.nextLine();

        if (input.equals("quit")){
            flag = false;
        } else {
            //Your code here
        }
    }

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