我如何使用户输入到for循环中。并使程序循环,直到用户输入quit

问题描述 投票:0回答:1

如何将用户输入到for循环中。并使程序循环直到用户输入退出。这是我的代码:

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));
}
java for-loop while-loop java.util.scanner user-input
1个回答
0
投票

在for循环内尝试一下:

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

        if (input == "quit"){
            flag =false;
        } else {
            //Your code here
        }
© www.soinside.com 2019 - 2024. All rights reserved.