我怎么能算奇数,双数和零在Java中的多位数的号码吗?(How can I count the nu

2019-09-28 02:47发布

基本上我需要编写一个程序,它的用户输入至多并包括2^31 -1中的整数的形式,并返回奇数,偶数和零数的量在INT。 例如,

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

要么

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

我敢肯定,我思前想它,但我不能慢,我的大脑了。 任何人都可以帮忙吗? 这是代码的第三次迭代,前两个已尝试与循环。

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

对不起,该代码在文本框中古怪格式化。

Answer 1:

 value = value % 10;

也许最终都待您的所有问题。

如果value2034 ,则value % 10的回报4 ...然后将该值分配给value ,你通过你if else块,然后做4/10得到0 ,并退出while loop而不解决其他的3个数字。

我建议更多的东西是这样的:

while (value > 0) {

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

    value /= 10;
}

或者, int thisDigit = value % 10然后替换value在当前if else与块thisDigit



Answer 2:

value = value % 10;

该声明将覆盖原来的value与一个提醒,即value % 10

如果value = 2034value % 10 = 4 ,则value = 4这是不是你想要的。

而是使用一个临时变量

int lastDigit = value % 10;

然后你的代码变得;

while (value > 0) {

    int lastDigit = value % 10;

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


Answer 3:

进口java.util.Scanner的;

公共类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");



 }

}      

}



Answer 4:

如果有些人仍然无法走出算出这个代码,我发现这个而有点摸索,和工作得很好:

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.");

    }
}


文章来源: How can I count the number of odd, evens, and zeros in a multiple digit number in Java?
标签: java count zero