2016-02-01 52 views
0

我嘗試在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
很高興收到任何答案!

回答

2

當你撥打nextInt,它需要一個項目掃描儀。這意味着一次通過循環會刪除多達三個項目,並且添加的項目與您正在進行檢查的項目不同。

想象一下你的輸入是4 3 1

您的代碼將做到這一點:

if ((scanner.nextInt() /* 4 */ % 2) == 0 && scanner.nextInt() /* 3 */ != 1) 
    myInts.add(scanner.nextInt() /* 1 */); 

並添加1到列表中。

您應該將代碼改成這樣:

while (scanner.hasNext()) 
{ 
    int value = scanner.nextInt(); 
    if ((value % 2) == 0) 
     myInts.add(value); 
} 

這將只讀取一個值,並在所有的比較使用它。

3

原因是每個新呼叫nextInt()都從輸入中讀取新的整數。

下面是修改後的代碼片段,說明你可能想嘗試什麼:

Scanner scanner = new Scanner(inputStream); 
int myInt; 

while (scanner.hasNext()) { 
    myInt = scanner.nextInt(); 

    if ((myInt % 2) == 0 && myInt != 1) 
    myInts.add(myInt); 
} 

欲瞭解更多信息,看看docs

0

每次撥打scanner.nextInt()時,都會得到另一個號碼。如果你想多次引用相同的數字,分配給一個變量。

此外,在檢查一個數是偶數,你不也得檢查它是不是數字1

while (scanner.hasNext()) { 
    int n = scanner.nextInt(); 
    if (n%2 == 0) { 
     myInts.add(n); 
    } 
} 
1

這裏的問題在於

if ((scanner.nextInt() % 2) == 0 && scanner.nextInt() != 1) 

每次撥打scanner.nextInt()時,都會消耗下一個輸入。正因爲如此,你最終會放棄大部分的輸入。要解決這個問題,你需要像

while (scanner.hasNext()) 
    { 
     int i = scanner.nextInt; 
     if ((i % 2) == 0 && i != 1) 
      myInts.add(i); 
    } 

這將正確地消耗輸入,並應該正常工作。 掃描儀的javadoc,其中包含此信息,在這裏找到:https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

-1

在行

if ((scanner.nextInt() % 2) == 0 && scanner.nextInt() != 1) 

您從輸入讀取兩個整數,而不是檢查同一個兩次:

int nextInt = scanner.nextInt(); 
if ((nextInt % 2) == 0 && nextInt != 1) 
1

是的我認爲你已經理解錯了。無論何時使用掃描器類指針的nextInt()方法,該掃描文件將移動到nextInt()。所以最好將該整數值保存在臨時變量中。以下是您的代碼的修改,

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()) 
    { 
     int firstNumber = scanner.nextInt(); 

     if ((firstNumber % 2) == 0 && firstNumber != 1) 
      myInts.add(firstNumber); 
     //if ((scanner.nextInt() & 1) == 0) 
     // myInts.add(scanner.nextInt()); 
    } 
    for (Integer x : myInts) 
    { 
     System.out.println(x); 
    }