开发者

Calculations coming out to 0.0?

开发者 https://www.devze.com 2023-01-01 22:29 出处:网络
A simple percentage calculation. It wont return a value except 0.0 and I think once or twice it returned 100.0%. Other than that it won\'t do a thing. I have tried playing with the code in several dif

A simple percentage calculation. It wont return a value except 0.0 and I think once or twice it returned 100.0%. Other than that it won't do a thing. I have tried playing with the code in several different ways and it just wont work.

for (int loop = 1; loop < loopCount; loop++)
            {
                aa = r.nextInt(10+1);
                abc = (int) aa;

                String[] userAnswer = new String[x];

                int totalQues = (correctAnswer + wrongAnswer), actualQues = (totalQues - 1);

                if(abc < x)
                {
                    userAnswer[abc] = JOptionPane.showInputDialog(null,"Question "+quesNum+"\n\n"+questions[abc]+"\n\nA: "+a[abc]+"\nB: "+b[abc]+"\nC: "+c[abc]+"\nD: "+d[abc]+"\nCorrect Answers: "+correctAnswer+"\nWrong Answers: "+wrongAnswer+"\nTotal Questions: "+totalQues);

                    if(userAnswer[abc].equals(answers[abc]))
                    {
                        correctAnswer++;
                    }
                    else
                    {
                        wrongAnswer++;
                    }//else

                    if(actualQues == x);
                    {
         开发者_开发问答               score = (correctAnswer / actualQues) * 100;

                        JOptionPane.showMessageDialog(null,"The test is finished.");

                        JOptionPane.showMessageDialog(null,"You scored "+score+"%");

                    }//if

                }//if

            }//for


Change

score = (correctAnswer / actualQues) * 100;

to

score = ((double)correctAnswer / actualQues) * 100;


Since you are doing integer division, the result of that operation will always be either 0 or 1. Instead, try casting to a floating-point number prior to dividing:

score = (int)((correctAnswer / (1.0 * totalQues)) * 100);


Assuming both correctAnswer and totalQues are integer types, the line score = (correctAnswer / totalQues) * 100; will use integer division (resulting in 0 or 1). To avoid this, you need to explicitly cast one to a floating type:

score = (correctAnswer / ((double)totalQues)) * 100;


score = (correctAnswer / actualQues)

If both correctAnswer and actualQues are int's then score will get 0 every time, because int values can't handle decimal values, thats why we use floating point.

correctAnswer and/or totalQues should be float/ double ( BigDecimal to be pedantic). That way, with one of the operands being a floating point value, the division is done in floating point arithmetic, instead of integer arithmetic..and SHAZAM!, you'll get a non-zero (non-truncated) result.


Cast (or even declare) the attributes to "double" type. That should work out for you.


If you don't need, or want a double, the simplest solution is to do the multiplcations first.

score = 100 * correctAnswer / actualQues;

In this case, score remains an int value.

0

精彩评论

暂无评论...
验证码 换一张
取 消