0

I am fairly new to Java and I have an assignment that I have to create 5 questions and answers and have a user answer them. The program should then output the correct results out of five, as well as the percent and a feedback message. My code is as follows.

package quiz;
import javax.swing.*;

public class Quiz {

    public static void main(String[] args) {

        int count = 0;
        final int totalQuestions = 5;

        JOptionPane.showMessageDialog(null, "Welcome to the Trivia Program!");
        JOptionPane.showMessageDialog(null, "A series of questions will be asked."
                + "\nPlease enter the letter of the answer"
                + "\nyou think is the correct choice.");

        String[][] question = {
            {"What letter is used to represent the number 100 in Roman Numerals?"
                + "\nA. M"
                + "\nB. D"
                + "\nC. C"
                + "\nD. X", "c"
            },
            {"Triton is the moon to which planet?"
                + "\nA. Jupiter"
                + "\nB. Neptune"
                + "\nC. Saturn"
                + "\nD. Uranus", "b"
            },
            {"What are Lanthanides?"
                + "\nA. Elements of the periodic table"
                + "\nB. Mountains on Earth"
                + "\nC. Mountains on the moon"
                + "\nD. Bacterium", "a"
            },
            {"What does a nidoligist study?"
                + "\nA. waves"
                + "\nB. clouds"
                + "\nC. bird nests"
                + "\nD. caves", "c"
            },
            {"Hypermetropic people are what?"
                + "\nA. obese"
                + "\nB. underfed"
                + "\nC. moody"
                + "\nD. far sighted", "d"
            }
        };

        String[] answers = new String[totalQuestions];

        for (int x = 0; x < totalQuestions; x++) {

            answers[x] = JOptionPane.showInputDialog("\t\t" + (x + 1) + ". " + question[x][0] + "   ");
            answers[x] = answers[x].toLowerCase();

            if (question[x][1].equals(answers[x])) {
                JOptionPane.showMessageDialog(null, "Correct!");
                count++;
            } else {
                JOptionPane.showMessageDialog(null, "Incorrect");
            }
        }

        int avg = (count / totalQuestions) * 100;

        switch (count) {
            case 3:
                JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct"
                        + "(" + avg + "%)"
                        + "\nAverage");
                break;
            case 4:
                JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct"
                        + "(" + avg + "%)"
                        + "\nVery Good");
                break;

            case 5:
                JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct"
                        + "(" + avg + "%)"
                        + "\nExcellent!");
                break;

            default:
                JOptionPane.showMessageDialog(null, "You got " + count + "/" + totalQuestions + " correct "
                        + "(" + avg + "%)"
                        + "\nPoor");
                break;
        }
    }
}

My problem is that it only outputs the correct average % when all answers are correct (5/5). Anything else and the percentage is 0. Can anyone help explain this to me?

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • 1
    change count & avg to be float – keety Oct 10 '14 at 02:57
  • when I change count to float, the switch statement says I can't use value float. When I change totalQuestions to float, the new string object that uses totalQuestions says cant convert from float to int. – Michael Dunn Oct 10 '14 at 03:05
  • Ok, I figured it out! I added (float) before count and totalQuestion in the avg equation. – Michael Dunn Oct 10 '14 at 03:13

2 Answers2

0

You need to change this line of code from:

int avg = (count / totalQuestions) * 100;

to

float avg= ((float)count/(float)totalQuestions) * 100;

edits:
To get the format like XX.XX%, you need to use string formatter:

String.format("%,.2f", avg);
rose000
  • 73
  • 8
  • ok, I did this and it worked, but when I get to 3/5 correct it outputs as 60.000004%. how would I fix this? not sure how to do this in a JOptionPane – Michael Dunn Oct 10 '14 at 03:20
0

When you divide two integers in Java, the result is an integer. In order to get a percentage, you can cast the integers into floats/doubles like this

int avg = (int)((((double)count) / ((double)totalQuestions)) * 100.0)
user2910265
  • 844
  • 1
  • 9
  • 15