سؤال

In the given code,

Input:
1
7

Output:
1

Output should be 7. What am I missing??

import java.io.*;
import java.util.*;

class compiler 
{
    public static void main(String args[] ) throws Exception 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int testcases = Integer.parseInt(br.readLine());

        //  System.out.println("If this statement is added it works as expected!")

        int n=Integer.parseInt(br.readLine());

        System.out.println(n);
    }
}

*I also tried using scanner

هل كانت مفيدة؟

المحلول

This should work. if you are not using an IDE, then maybe you changed your code and didn't compile again. so you are essentially running an old code with different output?

نصائح أخرى

In fact, after run your code, testcases will be 1 and n will be 7 (this will be printed on console - your example doesn't match output given).

Maybe this fragment of code will help with printing last given input to output:

int n;
while(br.readLine())
{
    n=Integer.parseInt(br.readLine());
}
System.out.println(n);
 The code must be like this if you want to use command line argument
 Public static void main(String ar[])
 {

  int i=(0;
  for(i=0;i<ar.length;i++)
  System.out.println(" "+ar[i]);

    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top