jeudi 2 février 2017

How to perform correct loop for Multiplication quiz

I'm new to programming and would like some help.

I'm trying to create a Multiplication quiz that loops infinitely until the user inputs no.

also, when a multiplication problem is presented the program must also display rather or not the user input is correct

When the user inputs no the loop will stop and display how many correct answers out of the total amount of questions asked.

any ideas?

Here is my code:

public static void main(String[] args) 
{
    int correctCount = 0; // count the number correct question
    int count = 0; // count the number of question
    Scanner input = new Scanner(System.in);


    //2 random single-digit int
    int number1 = (int)(Math.random() * 10);
    int number2 = (int)(Math.random() * 10);    


    do
    {
        //prompt the student to answer "what is number1 - number2
        System.out.print("What is " + number1 + " * " + number2 + "?");
        int answer = input.nextInt();

    //grade the answer and display the result
    if (number1 * number2 == answer)
    {
        System.out.println("You are correct!");
        correctCount++; //increase the correct answer count

    }
    else
    {
        System.out.println("Your answer is worng. \n" + 
    number1 + " * " + number2 + " should be: " + (number1 * number2));
    }

    count++;

    System.out.println("\nWould you like more questions?");
    System.out.print(" Y or N ?");
    String option = input.nextLine();
    } while (input.equals('y'));




     System.out.println("\nYou've answered " + correctCount + " out of " + count 
             + " Correct");

I know my code is a bit of a mess because I tried to perform do-while loop but wasnt able to get my program to run correctly.

Aucun commentaire:

Enregistrer un commentaire