计算不存在的东西?

时间:2014-01-11 02:06:10

标签: java math count bluej

当我编译并输入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);

2 个答案:

答案 0 :(得分:1)

我猜你的问题就在这里:

count = evenCount ++ + oddCount ++;

答案 1 :(得分:0)

下面:

count = evenCount++ + oddCount++;

你正在增加evenCount和oddCount。相反,我可以建议以下内容:

count = evenCount + oddCount;

另外,为什么你先用10计算模数,然后用2?

计算模数

示例:

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

示例的第一行无用,因为:

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

因此,您不需要为五个变量计算10的模数。这是值得的。

另外,如果某些东西不起作用且你正在寻求帮助,那么很可能你至少犯了一个错误。