views:

135

answers:

6

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
+1  A: 
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.

Tom
thanks! That solved it \m/
Nick Gibson
+4  A: 

Change

score = (correctAnswer / actualQues) * 100;

to

score = ((double)correctAnswer / actualQues) * 100;
Jonathon
+3  A: 

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);
Justin Ethier
+2  A: 

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;
VeeArr
A: 

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

arcamax
A: 

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.

Peter Lawrey