在线评判中的运行时错误

时间:2014-04-13 05:47:13

标签: java file-io

我正在编码一个简单的问题,其中我只需要在输入文件的行中对两个数字求和,并且在输出文件中必须打印两个数字之和的结果。

示例输入如下: -

5                          //The first line is the number of test cases
22 12
23 12
1 1
2 3
100 100

示例输出如下

 34
 35
 2
 5
 200  

以下是我的工作方式: -

import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;

public class Test131009 {
   public static void main(String[] args)throws Exception{

      Scanner in = new Scanner(new File("input.txt"));
      PrintWriter out = new PrintWriter(new File("main.out"));

     int T = in.nextInt();

     for(int t = 1; t <= T; t++) {
        int first = in.nextInt();
        int second = in.nextInt();
        int result = first + second;
        out.println(result);
     }
     out.close();
  }
}

我无法弄清楚为什么法官判定存在运行时错误。

1 个答案:

答案 0 :(得分:0)

这段代码对我有用。我只需要将输入从File更改为System.in并将输出写入System.out

这是代码。

import java.util.Scanner;


public class Main {
  public static void main(String[] args)throws Exception{

     Scanner in = new Scanner(System.in);

    int T = in.nextInt();

    for(int t = 1; t <= T; t++) {
      int first = in.nextInt();
       int second = in.nextInt();
       int result = first + second;
       System.out.println(result);
    }

 }
}