繁体   English   中英

如何让用户输入进入for循环并使程序循环直到用户输入退出

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

如何让用户输入进入 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));
}

在 for 循环中试试这个:

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

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

暂无
暂无

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

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