我將此代碼提交給KATTIS在線代碼測試器。它應該返回已經重複給出的兩個數字的差值,直到達到EOF。它在日食中效果很好,但是KATTIS說「沒有發現異常」。我希望得到一些幫助,以瞭解哪些例外未被捕獲。Java:未捕獲異常錯誤?
提供了輸入和類「Kattio」,所以輸入和輸出將始終與在線代碼系統一起工作。
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStream;
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int getInt() {
return Integer.parseInt(nextToken());
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
public class Hello {
public static void main(String[] args) {
Kattio io = new Kattio(System.in, System.out);
while (io.hasMoreTokens()){
int n1 = io.getInt();
int n2 = io.getInt();
if (n1>n2){
io.println(n1-n2);
}
else if (n2>n1){
io.println(n2-n1);
}
else {
io.println("0");
}
}
io.close();
}
}