为什么System.out.println()在Java中不起作用

问题描述 投票:-1回答:1

我的代码在我的电脑上运行良好,没有任何错误,但是当我在CodeChef的在线编译器上运行它时,它成功执行了,但没有打印任何输出。当我使用Scanner类进行输出时,它正在打印输出输入,但是它给了TLE,因此我使用bufferreader类来减少输入的运行时间,但是它停止打印输出,但是在我的电脑中给出了输出。

     static int profit = 0;

    public static void main(String[] args) throws IOException {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));

            int t = Integer.parseInt(br.readLine());
            while (t-- > 0) {
                int n = Integer.parseInt(br.readLine());
                int[] p = new int[n];
                for (int i = 0; i < n; i++) {
                    p[i] = Integer.parseInt(br.readLine());
                }
                Arrays.sort(p);
                int count = 0;
                while (n > 0) {
                    if (n - 1 == 0) {
                        //System.out.println("profit p[0]: " + profit + " " + " " + p[0] + " " + n);
                        profit = profit + p[0];
                        //System.out.println("profit after: " + profit);
                    } else {
                        for (int i = 0; i < n - 1; i++) {
                            if (p[i] == 0) {
                                p[i] = 0;
                            } else {
                                p[i] = p[i] - 1;
                            }
                            count++;
                        }
                        profit += p[n - 1];
                        //System.out.println("profit after: " + profit);

                    }
                    n = n - 1;


                }
                System.out.println(profit);
                profit = 0;
            }
        } catch (Exception e) {
            return;
        }
    }
java io bufferedreader
1个回答
0
投票

我认为我们没有足够的信息来找出CodeChef出了什么问题。但是,您的代码中有一个BIG错误,可能会使调试变得困难。

    } catch (Exception e) {
        return;
    }

这完全是错误的。您正在捕获所有可能的错误,并导致程序退出而没有任何错误消息。这是throwing信息,可帮助您弄清异常的含义,异常的发生位置以及(在进行一些分析之后)如何解决。

至少要这样做:

   } catch (Exception e) {
        e.printStackTrace();
        return;  // optional
   }

修复此问题,然后查看是否现在获得堆栈跟踪。

课程:不要再这样做了。曾经这等效于用脚射击自己。

© www.soinside.com 2019 - 2024. All rights reserved.