簡體   English   中英

for循環后的打印語句不打印,為什么?

[英]The print statement after for loop is not printing, why?

//'main' method must be in a class 'Rextester'.
//Compiler version 1.8.0_111

import java.util.*;
import java.lang.*;

class Rextester
{  
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int number;
        for(int i=0;i<=n;i++){
            int count=0;
            number=in.nextInt();
            while(number!=0){
                count++;
                number=number/10;
                }System.out.println(count);

        }System.out.println(n);//this does not get printed
    }
}

n 應該打印輸入的 n。為什么在 for 循環之后沒有打印 n? 它被打印在 for 循環中

我認為您認為它沒有打印的原因是因為您的 for 循環比您預期的多運行了一次迭代。

假設您輸入1 ,然后輸入999 ,則 output 為3 此時,您希望循環結束並再次打印1 (即n ),對嗎? 但是由於您的 for 循環 header 說int i=0;i<=n ,它實際上會運行 (n+1) 次。 在輸出999之后,循環還沒有完成,正在等待您的再次輸入。 這可能會讓您認為程序已經結束而沒有打印n

您應該將 for 循環條件更改為i < n

這只會在您的 for 循環退出后打印,所以如果您希望每次都打印它,只需將它移到 for 中,如下所示:

public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int number;
        for (int i = 0; i <= n; i++) {
            int count = 0;
            number = in.nextInt();
            while (number != 0) {
                count++;
                number = number / 10;
            }
            System.out.println(count);
            System.out.println(n);
        }
    }

您的代碼按預期工作。您應該在使用 in.nextInt(); 之前添加 sysout; 這樣您就會知道程序正在等待用戶輸入。請檢查以下代碼。

public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number - ");
        int n = in.nextInt();
        int number;
        for (int i = 0; i <= n; i++) {
            int count = 0;
            System.out.println("Enter another number - ");
            number = in.nextInt();
            while (number != 0) {
                count++;
                number = number / 10;
            }
            System.out.println("Count - "+count);
        }
        System.out.println("N is "+ n);
    }

output 如下所示。

Enter a number - 
5
Enter another number - 
5
Count - 1
Enter another number - 
5
Count - 1
Enter another number - 
5
Count - 1
Enter another number - 
5
Count - 1
Enter another number - 
5
Count - 1
Enter another number - 
5
Count - 1
N is 5
Your Loop is working. According to your code you have to give two input values. Have put comments in your code where you request the inputs. According to the code the loop will run number of times which you enter as the **input 1(n)** then each an every looping you have to enter the **input 2(number)** value


import java.util.*;
import java.lang.*;

class Rextester
{  
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in); //Input 1
        int n=in.nextInt();
        int number;
        for(int i=0;i<=n;i++){
            int count=0;
            number=in.nextInt(); //Input 2
            while(number!=0){
                count++;
                number=number/10;
                }System.out.println(count);

        }System.out.println(n);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM