簡體   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