2014-01-11 54 views
0

當我編譯並輸入5個平均值或5個賠率時,計數出現爲6個平均值和1個奇數或6個賠率和1個偶數。我認爲我做了一切正確的事情,請幫助明天的晚上。對不起,在這一點上,我寫這只是爲了讓它提交。計數不在那裏的東西?

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 = five % 10; 
if (five%2==0) 

的例子中的第一行是沒用的,因爲:

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

所以你不需要計算與十模爲你的五個變量。這是毫無價值的。

此外,如果有什麼不工作,你正在尋求幫助,那麼很可能你至少犯了一個錯誤。

相關問題