0

This is another school project where the main class generates two random numbers and then displays them as an addition problem. The user then has to enter the sum of the two numbers. The program should compare the input to the correct answer but every time the correct answer is put in, it says it is wrong?

Main Class:

import java.util.Random;

public class MathQuiz
{
private int answer1, answer2, answer3, answer4, answer5, n1, n2, sum, score,           scoreFinal;
private String message, m2, m3, m4, m5, finalPage;
public MathQuiz()
{
}

Random dice1 = new Random();

public void setN1()
{
    n1 = dice1.nextInt(100);
}

public void setN2()
{
    n2 = dice1.nextInt(100);
}

public void setSum()
{
    sum = n1+ n2;
}

public int getN1()
{
    return n1;
}

public int getN2()
{
    return n2;
}

public int getSum()
{
    return sum;
}



public String getAnswer1(String a1)
{
    if (a1.equals(getSum()))
    {
        message = "Correct";
        score++;
    }

    else 
    {
        message = "Incorrect";
    }

    return message;
}

public String getAnswer2(String a2)
{
    if (a2.equals(getSum()))
    {
        m2 = "Correct";
        score++;
    }

    else
    {
        m2 = "Incorrect";
    }

    return m2;
}

public String getAnswer3(String a3)
{
    if (a3.equals(getSum()))
    {
        m3 = "Correct";
        score++;
    }

    else
    {
        m3 = "Incorrect";
    }

    return m3;
}

public String getAnswer4(String a4)
{
    if (a4.equals(getSum()))
    {
        m4 = "Correct";
        score++;
    }

    else
    {
        m4 = "Incorrect";
    }

    return m4;
}

public String getAnswer5(String a5)
{
    if (a5.equals(getSum()))
    {
        m5 = "Correct";
        score++;
    }

    else
    {
        m5 = "Incorrect";
    }

    return m5;
}

public String getLast()
{
    scoreFinal = score * 20;
    finalPage = "Finished! Your score is " + scoreFinal + "%"; 
    return finalPage;
}
}

JOptionPane:

import javax.swing.JOptionPane;

public class Dialog
{
public static void main(String[] args)
{
    String A1, A2, A3, A4, A5;
    int a1, a2, a3, a4, a5;

    MathQuiz quiz = new MathQuiz();

    JOptionPane.showMessageDialog(null, "Answer these 5 math questions...");

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A1 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A1));

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A2 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A2));

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A3 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A3));

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A4 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A4));

    quiz.setN1();
    quiz.setN2();
    quiz.setSum();
    A5 = JOptionPane.showInputDialog(quiz.getN1() + " + " + quiz.getN2());

    JOptionPane.showMessageDialog(null, quiz.getAnswer1(A5));

    JOptionPane.showMessageDialog(null, quiz.getLast());
}
}
4

5 回答 5

4

传递给方法的答案getAnswerX()是字符串,您将它们与整数进行比较。String类的方法.equals()返回 false,因为参数是不同的类型,一个 int 原始类型。您可以使用 Integer.parseInt() 将字符串转换为整数,也可以在比较之前使用 Integer.toString() 将整数转换为字符串。

这样的事情应该会更好:

public String getAnswer1(String a1)
{
    if (Integer.parseInt(a1) == getSum())
    {
        message = "Correct";
        score++;
    }

    else 
    {
        message = "Incorrect";
    }

    return message;
}
于 2013-04-11T00:36:34.633 回答
0

您正在将代码中的 a 与 aString进行比较int,它们不相等。尝试改变你的比较

a1.equals(getSum())

a1.equals(getSum().toString())

为了将字符串与字符串进行比较。

于 2013-04-11T00:39:04.433 回答
0

您正在尝试将整数与字符串进行比较

尝试改变

a1.equals(getSum())

a1.equals(String.valueOf(getSum()))

http://www.tutorialspoint.com/java/java_string_valueof.htm

于 2013-04-11T00:41:53.843 回答
0

第 1 步:不要让用户问一组五个问题,而是问他们一个问题。这将极大地加快调试速度。一旦你的代码工作,提出五个问题。

第 2 步:代码只询问一个问题,然后您可以进行更简单的更改,即 Integer.toString() 并计算答案,如 Asaph 所述。你不会感觉到性能受到影响。

于 2013-04-11T00:44:20.843 回答
0

在撰写本文时,有两个答案,两个都是正确的。尝试某事的第一份好工作。许多人来这里寻找专业人士为他们做功课。

以下是一些需要考虑的事项来改进您的代码:

1)一致的缩进将帮助您阅读代码。2)为什么需要这么多getAnswer方法,你只需要一个,而不是1-5个。3)你应该检查你的getAnswer方法的String参数是否为空,否则你会得到一个空指针异常。

还有一些其他的事情,但现在可以。

于 2013-04-11T00:45:40.473 回答