当我用Scanner
其运行正常但我用的是Integer.parseInt(args[0])
有错误。为什么我们不能使用Integer.parseInt(args [0])代替扫描器
Scanner sc = new Scanner(System.in);
try {
int row = sc.nextInt();
System.out.println("please enter col ");
int col = sc.nextInt();
sc.close();
for (int i = 1;i<= row; i++) {
for (int j = 1; j <= col; j++) {
System.out.print("B");
}
System.out.println();
}
} catch(NumberFormatException e) {
e.printStackTrace();
}
System.out.println();
通过使用Integer.parseInt()
有错误。我不明白Integer.parseInt(args[0])
也转换为整数。
try{
for (int i = 1;i<= Integer.parseInt(args[0]); i++) {
for (int j = 1; j <= Integer.parseInt(args[1]; j++) {
System.out.print("B");
}
System.out.println();
}
} catch(NumberFormatException e) {
e.printStackTrace();
}
System.out.println();
这些代码片段做了根本不同的事情 - 你在比较苹果和橘子。 –
这段代码抛出什么错误? –
因为扫描器读取'System.in',而'args [0]'没有(它是您程序的初始运行时参数)。 – dasblinkenlight