简体   繁体   中英

Counting amount of even and odd numbers

I can not get this program to run right. After I enter the numbers nothing happens. Can someone point out where I am going wrong?

import java.util.Scanner;

public class EvenOdd {
  public static void main(String[] args) {

    //Declare variables
    int number;
    int evenNumbers = 0;
    int oddNumbers = 0;
    String answer = " ";

    //Create Scanner
    Scanner input = new Scanner(System.in);  

    do {

    //Prompt the user to enter a list of positive numbers with the last being a negative
    System.out.println("Please enter a list of positive numbers separated by a space.");
    System.out.println("(Enter a negative number after all positive numbers have been entered.)");

    //Read the users numbers
    number = input.nextInt();

    //An if statement determing a number either even or odd
    while (number >= 0) {

      if (number % 2 == 0) {

        evenNumbers++;

      } else {

        oddNumbers++;

      }//end of else

    //Read next number
    number = input.nextInt();

    }//end of while

    //Display total number of even and odd integers
    System.out.println("The total number of even positive intergers is: " + evenNumbers);
    System.out.println("The total number of odd positive integers is: " + oddNumbers);

    //Ask the us if they would like to play again
    System.out.println("Would you like to play again? Please type: 'yes' or 'no': ");

    //Move scanner to next line
    input.nextLine();

    //Read the users input
    answer = input.nextLine();

    } while(answer.equalsIgnoreCase("yes") ); //end of do-while

  }//end of main
}//end of class

Please consider using nextLine() instead of nextInt() for better error handling scenario.

Because nextInt() will try to read the incoming input. It will see that this input is not an integer, and will definitely throw exception. However, the input is not cleared, it is still there. There will still be "abcxyz" in the buffer. So going back to the loop will cause it to try parsing the same "abcxyz" over and over.

Using nextLine() will atleast clear the buffer, so that the next input you read after an error is going to be the fresh input that's after the bad line you have entered.

I think in this case it is your expectations that are wrong. The program tells you to enter positive numbers separated by spaces followed by a negative number. I'm guessing you probably didn't enter the negative number which is why the loop:

while (number >= 0)

continues executing, waiting for a negative integer value that will trigger it to stop and move on.

Some minor issues that you may want to address:

  1. There's a spelling mistake when printing the number of even integers, the word "intergers" should be "integers"
  2. The initial prompt says "Please enter a list of positive numbers..." when in reality 0 is allowed and only integers are valid input. To be more accurate it should say "non-negative integers"
  3. Similar to #2, the second prompt says, "Enter a negative number after all positive numbers..." which is not quite accurate. It would be more accurate to say "Enter a negative integer after all non-negative integers..."
  4. Lastly, I'm guessing it is a requirement that your program only accepts non-negative integers as valid inputs, but if not then you should know that it is mathematically valid to say that a negative integer is odd or even.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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