简体   繁体   English

没有编译错误,但代码的输出错误(JCreator):

[英]No compilation error but output error for the code (JCreator):

Input 输入

t (no of lines), next t lines contains space separated numbers. t(行数),接下来的t行包含空格分隔的数字。

Output 产量

The output should return the sum of the numbers in each lines. 输出应返回每行中数字的总和。

Problem 问题

Have coded in JCreater, no compilation error! 用JCreater编码,没有编译错误! BUT the desired output is only obtained after uncommenting the commented lines, else the program is terminating abruptly and showing answers to be zero. 但是,只有在取消注释注释行之后才能获得所需的输出,否则程序将突然终止并显示答案为零。 And it is working only with System.out.println statement, any other bogus syntax is not getting the result! 而且它仅与System.out.println语句一起使用,其他任何虚假语法都无法获得结果!

Code

import java.io.BufferedReader;
import java.io.InputStreamReader;

class TestClass {
    public static void main(String args[] ) throws IOException
    {
        int t=0;
        BufferedReader StdIn = new BufferedReader(new InputStreamReader(System.in));
        try
        {
            String line = StdIn.readLine();
            t = Integer.parseInt(line);
        }
        catch (IOException e) 
        {
            System.err.println("Error: " + e);
        }
        int m=0;
        int result[]= new int [t];
        for(int q=0;q<t;q++)
        {
        //  System.out.println("LOOP: "+q);
            m=fun_num();
            result[q]=m;
        }
        for(int q=0;q<t;q++)
            System.out.println(result[q]);
    }
    public static int fun_num()
    {
        int i=0,o=0,j=0,m=1,s=0,sum=0;
        String inp=null;
        BufferedReader StdIn = new BufferedReader(new InputStreamReader(System.in));
        try
        {
            String line = StdIn.readLine();
            inp=line;
        }
        catch (IOException e) 
        {
            System.err.println("Error: " + e);
        }
        String inp2=" ";
        j=inp.length();
        for(i=0;i<j;i++)
        {
            if (inp.charAt(i)== inp2.charAt(0))
                m++;
        }
        int num_in_order[]= new int [m];
        int num=0;
        for(i=0;i<j;i++)
        {
            if (inp.charAt(i)== inp2.charAt(0))
            {
                num_in_order[s]=num;
                s++;
                num=0;
            }
            else
            {
                o=((int)(inp.charAt(i))-48);
                num=(num*10)+o;
            }
        }
        num_in_order[s]=num;
        for(i=0;i<m;i++)
            sum=sum+num_in_order[i];
        return (sum);
    }
}

I think the very first reader you create (StdIn), is buffering all the input, before the readers in fun_num() get a chance to look at it. 我认为您创建的第一个阅读器(StdIn)正在缓冲所有输入,然后fun_num()的阅读器fun_num()机会查看它。 Try passing StdIn as a parameter to your "fun_num" function instead of creating it every time. 尝试将StdIn作为参数传递给您的“ fun_num”函数,而不是每次都创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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