简体   繁体   English

扫描仪Java的偶数和奇数

[英]Even and odd numbers with scanner Java

I try to add only even numbers to ArrayList . 我尝试仅将偶数添加到ArrayList I work with file using scanner as the most proper tool in my opinion. 我认为使用扫描仪作为最合适的工具来处理文件。 Path to the file should be written in console. 该文件的路径应在控制台中写入。 Also I use 2 the most popular ways to define even numbers. 我也使用2种最受欢迎​​的方式来定义偶数。 The problem is - not only even numbers are adding to my ArrayList . 问题是-不仅偶数都添加到我的ArrayList There is my code: 有我的代码:

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);
}

I suppose I misunderstand something about Scanner . 我想我对Scanner误解。
Would be glad to receive any answers! 很高兴收到任何答案!

The reason is that every new call of nextInt() read new integer from input. 原因是nextInt()每个新调用都会从输入中读取新的整数。

Here is a modified code snippet that illustrates what you might want to try: 这是修改后的代码段,说明您可能想要尝试的操作:

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

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

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

For more information look at the docs . 有关更多信息,请参阅docs

Every time you call nextInt , it takes an item out of the scanner. 每次调用nextInt ,它都会从扫描仪中取出一个项目。 That means that one pass through your loop removes as many as three items, and the items being added are not the same as the ones you're doing your checks on. 这意味着一次遍历循环将删除多达三项,并且所添加的项与您要检查的项不同。

Imagine your input is 4 3 1 假设您的输入是4 3 1

Your code will do this: 您的代码将执行以下操作:

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

And add 1 to your list. 并将1添加到您的列表。

You should change your code to this: 您应该将代码更改为此:

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

This will only read a single value, and use it in all comparisons. 这只会读取一个值,并在所有比较中使用它。

The problem here lies in 这里的问题在于

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

Every time you call scanner.nextInt() , you consume the next input. 每次调用scanner.nextInt() ,都会消耗下一个输入。 Because of this, you end up discarding most of the input. 因此,您最终将放弃大部分输入。 To fix this, you would need to have something like 要解决此问题,您需要使用类似

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

This will properly consume the input and should work properly. 这将正确消耗输入,并且应正常工作。 The scanner javadoc, which contains this information, is found here: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html 包含此信息的扫描器javadoc可在以下位置找到: https : //docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Yes I think you have understood it wrong. 是的,我认为您理解错了。 Whenever you use nextInt() method of the scanner class pointer which scanning file will move to the nextInt(). 每当您使用扫描仪类指针的nextInt()方法时,哪个扫描文件就会移至nextInt()。 So it is better to save that integer values in temporary variable. 因此最好将整数值保存在临时变量中。 Below is the modification of your code, 下面是对代码的修改,

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);
    }

Every time you call scanner.nextInt() you get another number. 每次您调用scanner.nextInt()您都会得到另一个号码。 If you want to refer to the same number multiple times, assign to a variable. 如果要多次引用相同的数字,请分配给变量。

Also, having checked that a number is even, you don't also have to check that it isn't the number 1. 另外,检查数字是否为偶数后,也不必检查数字是否为1。

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

In the line 在行中

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

You are reading two integers from the input, instead of checking the same one twice: 您正在从输入中读取两个整数,而不是两次检查相同的整数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM