简体   繁体   中英

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? it gets printed inside the for loop

I think the reason why you think it is not printing is because your for loop runs one extra iteration than you expected.

Say if you entered 1 , then 999 , the output is 3 . At this point you expect the loop to be over and 1 (which is n ) to be printed again right? But since your for loop header says int i=0;i<=n , it will actually run (n+1) times. The loop hasn't finished yet, after outputting 999 , and is waiting for your input again. This might have made you think that the program has ended without printing n .

You should change the for loop condition to 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:

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(); 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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