找出奇数偶数和零

时间:2015-10-13 01:48:31

标签: java

我的java程序出了问题。我必须输入一个值然后打印出赔率,均衡和零的数量。赔率和零显示正常,但均衡显示总数。

import java.util.Scanner;

public class OddEvenZero 
{
public static void main(String[] args)
{
int even = 0;
int odd = 0;
int zero = 0;
int placeInValue;

Scanner scan = new Scanner(System.in);

System.out.println("Enter a Value: ");
String valueEntered = scan.nextLine();

for(placeInValue = 0; placeInValue < valueEntered.length(); placeInValue ++)
{
    char value = valueEntered.charAt(placeInValue);

    int numberUsedInLoop = Integer.parseInt(Character.toString(value));

    if(numberUsedInLoop == 0)
    {
        zero ++;
    }
    else if(numberUsedInLoop%2 == 0);
    {
        even ++;
    }
    if(numberUsedInLoop%2 != 0 && numberUsedInLoop != 0)
    {
        odd ++;
    }

}
System.out.println("Number of Zeroes in Number: " + zero);
System.out.println("Number of Evens in Number: " + even);
System.out.println("Number of Odds in Number: " + odd);
}
}

输出: 输入值: 225500 数量为零的零数:2 数量中的Evens数量:6 数量的赔率:2 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

分号会立即终止else if

else if(numberUsedInLoop%2 == 0); // <-- terminates the else if
{ // <-- raw block
    even ++;
}

将其更改为

else if(numberUsedInLoop%2 == 0)
{
    even ++;
}
else // <-- just an else should satisfy your conditions
{
    odd ++;
}