【发布时间】:2015-05-11 14:18:48
【问题描述】:
我一直试图弄清楚如何执行此代码,但无法弄清楚如何更改我当前的代码以使其正常工作。
问题如下:
编写一个程序从输入文件名“input5_01.txt”中读取一个整数 N (2
目前,这是我的代码:
public static final int UPPER = 99, LOWER = 10;
public static void main(String[] args) throws FileNotFoundException {
int n = 5;
int[][] arr = new int[n][n];
Scanner inputFile = new Scanner(new File("input5_01.txt"));
//read in an int (n) from input file
while (inputFile.hasNext()) {
if (inputFile.hasNextInt() >= 2 && inputFile.hasNextInt() <= 30) {
n = inputFile.nextInt();
} else {
inputFile.next();
}
}
//assigning random numbers to array
Random rand = new Random();
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[0].length; c++) {
arr[r][c] = rand.nextInt(UPPER - LOWER + 1) + LOWER;
}
System.out.println();
}
}
真的,我唯一的问题是:
if(inputFile.hasNextInt() >= 2 && inputFile.hasNextInt() <= 30)
我可以做些什么来完成这项工作?
【问题讨论】:
-
您遇到错误了吗?您当前的代码发生了什么?
-
作为最佳实践,请指定一个字符集:
new Scanner(new File("input5_01.txt"), "UTF-8");