Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
65 views
in Technique[技术] by (71.8m points)

java - How do i assign a new value?

I need to find a way where the user can enter decimal numbers not only whole numbers for the division part and I also need help to fix the multiplication part, it keeps displaying the wrong answer when I intentionally wrote the wrong answer, for me it kept saying "the correct answer is 140" when the question was 5 x 2, and when I write 10 it says its correct but if its the same question and I write 4, for example, is says wrong the correct answer is 140 to the 5 x 2 question please help? here's my full code, and please if you find any more mistakes I did please let me know.

public static void main(String[] args) {

    String name = JOptionPane.showInputDialog(null, "Please enter your name");
    JOptionPane.showMessageDialog(null, "Hello " + name + "
Welcome to your Math Quiz (no calculators allowed) 
Please answer the following questions");

    int randVal1 = (int) (20 * Math.random()) + 1;
    int randVal2 = (int) (20 * Math.random()) + 1;
    int randomNumberAdd = randVal1 + randVal2;
    int randomNumberMul = randVal1 * randVal2;
    int randomNumberDiv = randVal1 / randVal2;
    int correct = 0;
    for (int i = 1; i < 10; i++) {

        String userAnswer = JOptionPane.showInputDialog(null, randVal1 + " + " + randVal2 + " = ");
        int answer = parseInt(userAnswer);
        if (answer == randVal1 + randVal2) {
            JOptionPane.showMessageDialog(null, "Correct!");
            correct++;
        } else if (answer != randVal1 + randVal2) {
            JOptionPane.showMessageDialog(null, "Incorrect!");
            JOptionPane.showMessageDialog(null, "The correct answer is " + randomNumberAdd);
        }

        {
            randVal1 = (int)(20* Math.random()) + 1;
            randVal2 = (int)(20*Math.random()) + 1;
            String userAnswerMul = JOptionPane.showInputDialog(null, randVal1 + " x " + randVal2 + " = " );
            int answer2 = parseInt (userAnswerMul);
            if (answer2 == randVal1 * randVal2){
                JOptionPane.showMessageDialog(null, "Correct");
                correct++;
            } else if (answer2 != randVal1 * randVal2){
                JOptionPane.showMessageDialog(null, "Incorrect");
                JOptionPane.showMessageDialog(null, "The correct answer is " + randomNumberMul);
            }
            {
                randVal1 = (int) (10 * Math.random()) + 1;
                randVal2 = (int) (10 * Math.random()) + 1;
                String userAnswerDiv = JOptionPane.showInputDialog(null, randVal1 + " ÷ " + randVal2 + " = ");
                int answer3 = parseInt(userAnswerDiv);
                if (answer3 == randVal1 / randVal2) {
                    JOptionPane.showMessageDialog(null, "Correct!");
                    correct++;
                } else if (answer3 != randVal1 / randVal2) {
                    JOptionPane.showMessageDialog(null, "Wrong!");
                    JOptionPane.showMessageDialog(null, "The correct answer is " + randomNumberDiv);
                }
                {

                }
            }
        }
    }
    JOptionPane.showMessageDialog(null, "You got " + correct + " correct answers.");

}
}
question from:https://stackoverflow.com/questions/65894877/how-do-i-assign-a-new-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...