【发布时间】:2014-04-20 08:46:23
【问题描述】:
我正在尝试在 spoj 上提交第二个代码,但它给出了错误的答案,但第一个被接受了,尽管我认为这两个代码的逻辑是相同的。
public class Main {
public static void main(String[] args) throws java.lang.Exception {
java.io.BufferedReader r = new java.io.BufferedReader(
new java.io.InputStreamReader(System.in));
String s;
while (!(s = r.readLine()).startsWith("42"))
System.out.println(s);
}
}
和
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
if (n != 42) {
System.out.println(n);
}
}
}
【问题讨论】:
-
尝试使用
42xyz作为输入字符串执行两个sn-ps -
这两个代码的区别在于第二个代码尝试将String转换为数字,如果字符串不是数字,可能会导致数字格式异常