寻找百分比

时间:2014-01-11 01:37:43

标签: java numbers

我目前正在尝试查找奇数和偶数以及有多少奇数和偶数以及奇数和偶数的百分比。现在,每当我尝试输入它时,它表示百分比零,你能告诉我原因吗。

    int one = 0;
    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;

    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;
   }
   int count = 0;
   count = evenCount + oddCount;

   System.out.println(); 
   System.out.printf("Even: %d Odd: %d",evenCount, oddCount);

   int percentage = 0;
   percentage = evenCount / count;
   percentage = percentage * 100;
   System.out.println(" Percentage  of Even " + percentage);

   int oddpercentage = 0;
   oddpercentage = oddCount / count;
   oddpercentage = oddpercentage * 100;
   System.out.println(" Percentage  of Odd " + oddpercentage);

3 个答案:

答案 0 :(得分:2)

您得到0,因为您将百分比和奇数百分比变量声明为int数据类型。将它们更改为double数据类型。 分隔符编号也应为double

这是因为,任何除以较大整数的整数都会给你0。例如,1/2 = 02/5 = 099/100 = 0

然后,而不是这样做,

percentage = oddCount / count;
percentage = oddpercentage * 100;

您可以这样做:

percentage = oddCount * 100 / (double) count;

注意:

必须one = one % 10;检查奇数/偶数

这就够了:

if (one %2==0) {
   evenCount++;
}
else { 
   oddCount++;
}

删除您的while循环,根本不需要或根本不使用

答案 1 :(得分:0)

您将百分比定义为int。

这意味着你正在做(作为例子)

int percentage =(int)43/100 =(int)0.43 = 0

将此乘以100得到0 * 100 = 0。

尝试使用双倍然后再乘以100,看看你是否到达某个地方。 如果你特别想要只有一个整数,你可以使用你剩下的双精度(它将包含小数位),并做整数值。

答案 2 :(得分:0)

你得到1甚至6,因为你必须将百分比和oddpercentage都改为double(而不仅仅是百分比),就像这样

公共类X {

    /**
     * @param args
     */
    public static void main(String[] args) {
         int one = 0;
            int two = 0;
            int three = 0;
            int four = 0;
            int five = 0;

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

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

            oneString = "1";
            one = Integer.parseInt (oneString);
            while (one > 0) {
                one = one % 10;
                if (one%2==0) {
                    evenCount++;
                }
                else{ 
                    oddCount++;
                }
                one = one / 10;
            }
            twoString = "1";
            two = Integer.parseInt (twoString);

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

            while (three > 0) {
                three = three % 10;
                if (three%2==0){
                   evenCount++;
                }else{ 
                   oddCount++;
                }
            three = three / 10;
            }
            fourString = "3";
            four = Integer.parseInt (fourString);
            while (four > 0) {
                four = four % 10;
                if (four%2==0){
                evenCount++;
                }else{ 
                   oddCount++;
                }
                four = four / 10;
            }
            fiveString = "5";
            five = Integer.parseInt (fiveString);
            while (five > 0) {
                five = five % 10;
                if (five%2==0){
                   evenCount++;
                }else{ 
                   oddCount++;
                }
                five = five / 10;
           }
           int count = 0;
           count = evenCount + oddCount;

           System.out.println(); 
           System.out.printf("Even: %d Odd: %d",evenCount, oddCount);

           double percentage = 0; ///// HERE
           percentage = evenCount / count;
           percentage = percentage * 100;
           System.out.println(" Percentage  of Even " + percentage);

           double oddpercentage = 0; ///// AND HERE
           oddpercentage = oddCount / count;
           oddpercentage = oddpercentage * 100;
           System.out.println(" Percentage  of Odd " + oddpercentage);

    }