2014-01-11 2 views
0

이것을 컴파일하고 5 evens 또는 5 odds를 입력하면 count는 6 evens와 1 odd 또는 6 odds 그리고 1 even으로 나옵니다. 나는 모든 것이 바르게 끝났다고 생각한다. 죄송합니다. 지금이 글을 쓰고 싶습니다. 나는이 문제를 추측에는 요거기에없는 것들을 계산합니까?

String oneString=""; 
    String twoString=""; 
    String threeString=""; 
    String fourString=""; 
    String fiveString=""; 

    int evenCount = 0, oddCount = 0, zeroCount = 0; 

    oneString = JOptionPane.showInputDialog ("Input your first number"); 
    one = Integer.parseInt (oneString); 
    while (one > 0) { 
    one = one % 10; 
    if (one%2==0) 
    { 
     evenCount++; 
    } 
    else 
    { 
     oddCount++; 
    } 
    one = one/10; 
    } 
    twoString = JOptionPane.showInputDialog ("Input your second number"); 
    two = Integer.parseInt (twoString); 

    while (two > 0) { 
    two = two % 10; 
    if (two%2==0) 
    { 
     evenCount++; 
    } 
    else 
    { 
     oddCount++; 
    } 
    two = two/10; 
    } 
    threeString = JOptionPane.showInputDialog ("Input your third number"); 
    three = Integer.parseInt (threeString); 

    while (three > 0) { 
    three = three % 10; 
    if (three%2==0) 
    { 
     evenCount++; 
    } 
    else 
    { 
     oddCount++; 
    } 
    three = three/10; 
    } 
    fourString = JOptionPane.showInputDialog ("Input your fourth number"); 
    four = Integer.parseInt (fourString); 
    while (four > 0) { 
    four = four % 10; 
    if (four%2==0) 
    { 
     evenCount++; 
    } 
    else 
    { 
     oddCount++; 
    } 
    four = four/10; 
    } 
    fiveString = JOptionPane.showInputDialog ("Input your fifth number"); 
    five = Integer.parseInt (fiveString); 
    while (five > 0) 
    { 
     five = five % 10; 
     if (five%2==0) 
     { 
      evenCount++; 
     } 
     else 
     { 
      oddCount++; 
     } 
     five = five/10; 
    } 
    float count; 
    count = evenCount++ + oddCount++; 
    System.out.println(); 
    System.out.printf("Even: %d Odd: %d",evenCount, oddCount); 
    float percentage; 
    percentage = evenCount * 100/count; 
    System.out.println(" Percentage of Even " + percentage); 
    float oddpercentage; 
    oddpercentage = oddCount * 100/count; 
    System.out.println(" Percentage of Odd " + oddpercentage); 

답변

1

바로 여기에 있습니다 :

계수 = evenCount ++ + oddCount ++; 여기

0

는 :

count = evenCount++ + oddCount++; 

당신은 evenCount 및 oddCount 모두 증가하고있다. 대신 다음과 같이 제안 할 수 있습니다.

count = evenCount + oddCount; 

또한 왜 10을 먼저 모듈로 계산 한 다음 2로 모듈로를 계산 했습니까?

예 :

((five % 10) % 2) == five % 2 

그래서 당신은 당신의 다섯 개 가지 변수 열을 가진 모듈을 계산하는 데 필요하지 않습니다 때문에

five = five % 10; 
if (five%2==0) 

예제의 첫 번째 행은, 쓸모. 그것은 무엇보다 가치가 있습니다.

또한 작동하지 않고 도움을 요청하는 경우 대부분 실수를 저 지르려고합니다.

관련 문제