我嘗試在ArrayList
上添加偶數。在我看來,我使用掃描儀作爲最適合的工具。文件路徑應該寫在控制檯中。另外我用2種最流行的方法來定義偶數。 問題是 - 不僅偶數加在我的ArrayList
上。 有我的代碼:使用掃描儀的偶數和奇數Java
BufferedReader bfReader = new BufferedReader(new InputStreamReader(System.in));
InputStream inputStream = null;
List<Integer> myInts = new ArrayList<Integer>();
String filePath = null;
try {
filePath = bfReader.readLine();
inputStream = new FileInputStream(filePath);
} catch (IOException e) { }
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNext()) {
if ((scanner.nextInt() % 2) == 0 && scanner.nextInt() != 1)
myInts.add(scanner.nextInt());
// if ((scanner.nextInt() & 1) == 0)
// myInts.add(scanner.nextInt());
}
for (Integer x : myInts) {
System.out.println(x);
}
我想我誤解了一些關於Scanner
。
很高興收到任何答案!