Closed. This question is not reproducible or was caused by typos。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

4年前关闭。



Improve this question




我的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
偶数个数:6
赔率数目: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 ++;
}

10-08 02:54