java中的运行时错误
我正在 spoj 上做一个问题来计算可以放置在 n*n 板上而不互相攻击的主教的数量..
我决定使用 java ..但我的代码出现运行时错误..请帮助。
代码:
import java.math.BigInteger;
import java.util.*;
class Bishop {
public static void main(String[] args) throws java.lang.Exception {
Scanner scanner = new Scanner(System.in);
while (true) {
int n = scanner.nextInt(); /*Error occurs here*/
if (n == 1) {
System.out.println("1");
continue;
}
BigInteger bi = BigInteger.valueOf(n);
BigInteger c = bi.multiply(BigInteger.valueOf(2));
BigInteger d = c.subtract(BigInteger.valueOf(2));
System.out.println(d);
}
}
}
I'm doing a problem on spoj to calculate no of bishops that can be placed on a board of n*n without attacking each other..
I decided to use java for it.. but my code is getting runtime error..pls help.
Code:
import java.math.BigInteger;
import java.util.*;
class Bishop {
public static void main(String[] args) throws java.lang.Exception {
Scanner scanner = new Scanner(System.in);
while (true) {
int n = scanner.nextInt(); /*Error occurs here*/
if (n == 1) {
System.out.println("1");
continue;
}
BigInteger bi = BigInteger.valueOf(n);
BigInteger c = bi.multiply(BigInteger.valueOf(2));
BigInteger d = c.subtract(BigInteger.valueOf(2));
System.out.println(d);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
System.in 是一个InputStream,这意味着它读取二进制内容。除非您事先将其正确转换为文本,否则扫描仪将无法正常工作。特别是,如果您没有向 Scanner 提供 CharSet,它将使用您平台的默认字符集,该默认字符集可能有所不同。
或者,您可能希望使用 java.io.Console 从标准输入读取文本。
System.in is an InputStream, meaning it reads binary content. Unless you properly transform it to text beforehand, Scanner won't work correctly. In particular, if you don't provide Scanner with a CharSet, it will use your platform's default one, which may be different.
Alternatively, you may want to use the java.io.Console to read text from the standart input.
尝试以下操作,这只会检查扫描仪是否确实包含整数。
Try the following, this will just check that the scanner does indeed contain an integer.