Start New Discussion within our Software Development Community

...kindly help me with code doesn't work....
...create a program that will ask questions and at the end of the session it will display the total number of correct answers and the equivalent grade....


import javax.swing.JOptionPane;
public class questions {

    public static void main(String[] args) {

    int score=0;
    int score1=0;
    int score2=0;

    String q1=JOptionPane.showInputDialog(null,"one plus one is? ");
        if (q1=="two")
            else if(q1!="two")
    String q2=JOptionPane.showInputDialog(null,"four times four is? ");
        if (q2=="sixteen")
            else if(q1!="sixteen")
    int tscore=score1+score2;
    double grade=(tscore/2)* (100);
    JOptionPane.showMessageDialog(null,"Your score is "+ tscore +" over 2!" 
        + "\n" + "Your grade is " + grade);

if you want to match string agains string you need to use String method equals() or equalsIgnoreCase(String anotherString).
Secondly to store score you need only one variable and not four plus you DO NOT need else if part of statement! String is either what is correct answer and score get increased or it is not and score remain as it was...

This question has already been answered. Start a new discussion instead.