How can I count the number of odd, evens, and zero

2019-08-06 02:33发布

问题:

Basically I need to write a program that takes user input up to and including 2^31 -1 in the form of an integer and returns the amount of odd, even, and zero numbers in the int. For example,

Input: 100
Output: 1 Odd, 0 Even, 2 Zeros // 1(Odd)0(Zero)0(Zero)

or

Input: 2034
Output: 1 Odd, 2 Even, 1 Zero // 2(Even)0(Zero)3(Odd)4(Even)

I'm pretty sure I'm over thinking it but I can't slow my brain down. Can anyone help? This is the third iteration of the code, the first two were attempted with for loops.

import java.util.Scanner;

 public class oddEvenZero
 {
    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        int value;
        int evenCount = 0, oddCount = 0, zeroCount = 0;

        System.out.print("Enter an integer: ");
        value = scan.nextInt();

        while (value > 0) {

        value = value % 10;

        if (value==0) 
        {
           zeroCount++;
        }
        else if (value%2==0) 
        {
           evenCount++;
        }
        else 
        { 
           oddCount++;
        }
        value = value / 10;
    }
    System.out.println(); 
    System.out.printf("Even: %d Odd: %d Zero: %d", evenCount, oddCount, zeroCount);
 }
}

Sorry, the code formatted weirdly in the textbox.

回答1:

 value = value % 10;

Probably the end-all-be-all of your problems.

If value is 2034, then value % 10 returns 4... and then assigns that value to value, you go through your if else block, then do 4/10 get 0, and exit the while loop without addressing the other 3 digits.

I suggest something more like this:

while (value > 0) {

    if ((value%10)==0) {
       zeroCount++;
    }
    else if (value%2==0) { //As per comment below...
       evenCount++;
    }
    else { 
       oddCount++;
    }

    value /= 10;
}

Or, int thisDigit = value % 10, then replace value in your current if else block with thisDigit.



回答2:

value = value % 10;

This statement will override your original value with a reminder i.e value % 10.

If value = 2034 and value % 10 = 4, then value = 4 which isn't what you want.

Instead use a temporary variable

int lastDigit = value % 10;

Then your code becomes;

while (value > 0) {

    int lastDigit = value % 10;

    if (lastDigit==0) 
    {
       zeroCount++;
    }
    else if (lastDigit%2==0) 
    {
       evenCount++;
    }
    else 
    { 
       oddCount++;
    }
    value = value / 10;
}


回答3:

import java.util.Scanner;

public class oddEvenZero {

 public int[] convertStringArraytoIntArray(String[] sarray) throws Exception {


     if (sarray != null) 
        {
            int k= sarray.length-1;
            int intarray[] = new int[k];

                for (int i = 1; i < sarray.length; i++) {

                    intarray[i-1] = Integer.parseInt(sarray[i]);
                }

                return intarray;
        }
        return null;
        }


 public static void main(String[] args) {


    Scanner scan = new Scanner (System.in);

    String value;

    System.out.print("Enter an integer: ");

    value = scan.next();


    String words[] = value.split("");



    oddEvenZero obj = new oddEvenZero();

  try{

  int intarray[]=   obj.convertStringArraytoIntArray(words);

 int even_number =0;
 int odd_number =0;
 int zero_number =0;
  for (int h: intarray)
  {

      if(h==0)
      {
         zero_number++; 

      }
      else if(h%2==0)
      {

          even_number++;
      }

      else{

          odd_number++; 
      }
  }

 System.out.println("even numbers are"+ even_number);

 System.out.println("odd numbers are"+odd_number);
 System.out.println("Zero numbers are"+zero_number); 
  }


 catch(Exception ex)
 {

     System.out.println("Please enter number");



 }

}      

}



回答4:

If some of you are still unable to figure this code out, I found this while searching around for a bit, and works just fine:

import java.util.*;

public class Java_1
{
    public static void main (String[] args)
    {
        String string;
        int zero = 0, odd = 0, even = 0, length, left = 0;

        Scanner scan = new Scanner(System.in);

        System.out.print ("Enter any positive number: ");
        string = scan.next();

        length = string.length();

        while (left < length)
        {
            string.charAt(left);
            if (string.charAt(left) == 0)
                zero++;
            else if (string.charAt(left) % 2 == 0)
                even++;
            else
                odd++;

            left++;
        }

        System.out.println ("There are: "+ zero + " zeros.");
        System.out.println ("There are: "+ even + " even numbers.");
        System.out.println ("There are: "+ odd + " odd numbers.");

    }
}


标签: java count zero