简体   繁体   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 should print the n that is typed in. Why is n not getting printed after for loop? n 应该打印输入的 n。为什么在 for 循环之后没有打印 n? it gets printed inside the for loop它被打印在 for 循环中

I think the reason why you think it is not printing is because your for loop runs one extra iteration than you expected.我认为您认为它没有打印的原因是因为您的 for 循环比您预期的多运行了一次迭代。

Say if you entered 1 , then 999 , the output is 3 .假设您输入1 ,然后输入999 ,则 output 为3 At this point you expect the loop to be over and 1 (which is n ) to be printed again right?此时,您希望循环结束并再次打印1 (即n ),对吗? But since your for loop header says int i=0;i<=n , it will actually run (n+1) times.但是由于您的 for 循环 header 说int i=0;i<=n ,它实际上会运行 (n+1) 次。 The loop hasn't finished yet, after outputting 999 , and is waiting for your input again.在输出999之后,循环还没有完成,正在等待您的再次输入。 This might have made you think that the program has ended without printing n .这可能会让您认为程序已经结束而没有打印n

You should change the for loop condition to i < n .您应该将 for 循环条件更改为i < n

That will only be printed after your for loop exits, so if you want it to be printed everytime just move it inside the for like this:这只会在您的 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);
        }
    }

Your code is working as expected.You should add sysout before using the in.nextInt();您的代码按预期工作。您应该在使用 in.nextInt(); 之前添加 sysout; So that you will know program is waiting for user input.Please check the below code.这样您就会知道程序正在等待用户输入。请检查以下代码。

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);
    }

The output is like below. 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