0

When the student gets the question right, the program asks another random question. But when the student gets the question wrong, it still asks them a new question. I want to CHANGE it so that when the student gets the question wrong, it asks them the same question again until they get it right. PLEASE help, thank you so much!

import java.util.Random;
import java.util.Scanner;


public class Multiplication {

    static Random random = new Random();

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int correctCtr =0;
        int wrongCtr = 0;

        System.out.println("Welcome to the CAI-Knowledge Assessment\n ");

        int type = getProblemType(scanner);
        System.out.print("Enter difficulty level: ");
        int difficulty = scanner.nextInt();

        while (true) {
            int questionAns = getQuestion(type, difficulty);
            int answer = scanner.nextInt();

            if (questionAns != answer) {
                System.out.println(getWrongResponse());
                wrongCtr++;


            } else {
                System.out.println(getCorrectResponse());
                correctCtr++;
            }

            if ((wrongCtr + correctCtr) == 10) {
                double correctness = (correctCtr / 10.0) * 100;
                if (correctness > 75) {
                    System.out.println("Congratulations, you are ready to go to the next level!");
                } else {
                    System.out.println("Please ask your teacher for extra help.");
                }
                wrongCtr = 0;
                correctCtr = 0;
                while (true) {
                    System.out.print("Try again? Y or any other character to exit:");
                    String response = scanner.next();
                    if (response.equalsIgnoreCase("y")) {
                        type = getProblemType(scanner);
                        System.out.print("Enter difficulty level: ");
                        difficulty = scanner.nextInt();
                        break;
                    } else {
                        System.out.println("Thank you for learning!");
                        System.exit(0);
                    }
                }
            }

        }
    }

    static int getProblemType(Scanner scanner) {
        int type;
        while (true) {

            System.out.print("What type of problem would you like? \n(1=addition, 2=subtraction, 3=multiplication, 4=division, 5=mixture): ");
            type = scanner.nextInt();
            if (type > 0 && type < 6) {
                break;
            } else {
                System.out.println("Invalid type!");
            }
        }
        return type;
    }

    static String getCorrectResponse() {
        String response = null;
        int index = random.nextInt(4);
        switch (index) {
        case 0:
            response = "Very good!";
            break;
        case 1:
            response = "Excellent!";
            break;
        case 2:
            response = "Nice work!";
            break;
        case 3:
            response = "Keep up the good work!";
            break;
        }
        return response;
    }

    static String getWrongResponse() {
        String response = null;
        int index = random.nextInt(4);
        switch (index) {
        case 0:
            response = "No. Please try again.";
            break;
        case 1:
            response = "Wrong. Try once more.";
            break;
        case 2:
            response = "Don't give up!";
            break;
        case 3:
            response = "No. Keep trying.";
            break;
        }

        return response;
    }

    static int getQuestion(int type, int difficulty) {
        int no1 = random.nextInt((int)Math.pow(10, difficulty));
        int no2 = random.nextInt((int)Math.pow(10, difficulty));

        int returnVal = 0;
        if (type == 1) {
            System.out.printf("How much is %d plus %d? ",no1,no2);
            returnVal = no1 + no2;
        } else if (type == 2) {
            while (no2 > no1) {
                no2 = random.nextInt((int)Math.pow(10, difficulty));
            }
            System.out.printf("How much is %d minus %d? ",no1,no2);
            returnVal = no1 - no2;
        } else if (type == 3) {
            System.out.printf("How much is %d times %d? ",no1,no2);
            returnVal = no1 * no2;
        } else if (type == 4) {
            while (no2 == 0 || no2 > no1 || no1%no2!=0) {
                no2 = random.nextInt((int)Math.pow(10, difficulty));
            }
            System.out.printf("How much is %d divided by %d? ",no1,no2);
            returnVal = no1 * no2;
        } else if (type == 5) {
            returnVal = getQuestion(random.nextInt(5)+1, difficulty);
        }

        return returnVal;
    }
}
3
  • 7
    Is all of this code relevant to your question? If not, consider removing the parts that aren't. Commented Sep 28, 2013 at 2:23
  • Not entirely, I just didn't want to leave anything out that would be needed to help solve my problem. Commented Sep 28, 2013 at 2:27
  • When a student answers the same question wrong again, should counter of wrong answers be incremented? Commented Sep 28, 2013 at 2:35

6 Answers 6

1

You said it yourself

Set<String> questions = getAllQuestions();
for( String question : questions) {
   boolean correctAnswer = false;
   while(!correctAnswer) {
      correctAnswer = askQuestion(question);
   }
}

Now, just fill in the blanks. Implement the methods in the template algorithm. To avoid running into an infinite loop if the person does not know the correct answer, you may want to break the loop after certain number of attempts. I am sure you can add that logic very easily to the template algorithm now.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it multiple ways. A simple hack can be to iterate at the same point as shown-

int answer = scanner.nextInt();

if (questionAns != answer) {
          *while (questionAns != answer)*{
            wrongAnswer();
            System.out.println("Try Again");
            int answer = scanner.nextInt();
          }
        }

static void wrongAnswer(){
     System.out.println(getWrongResponse());
     wrongCtr++;
}   

P.S. I haven't tested the code. Good luck.

Comments

0

Just set another variable that will keep track of whether or not they've got the question correct, then only reset the question if that variable is false.

Boolean questionCorrect = true;
while (true) {
        if (questionCorrect) {
            int questionAns = getQuestion(type, difficulty);   
        }
        int answer = scanner.nextInt();

        if (questionAns != answer) {
            System.out.println(getWrongResponse());
            wrongCtr++;
            questionCorrect = false;

        } else {
            System.out.println(getCorrectResponse());
            correctCtr++;
            questionCorrect = true;
        }

Comments

0

As you randomly generate no1, no2. So, you should keep your question in global variable. Assign the variable in getQuestion(int type, int difficulty) method. Here is sample code.

String question=""

   static int getQuestion(int type, int difficulty){
     if(type==2){
       question=String.format("How much is %d minus %d? ",no1,no2);
      }
   }

Then you can ask the same question, if user get wrong.

Comments

0

You should simply cache an instance of questionAns (your int) and a string variable that when initialized has the same format as what you are printing to output (you can do this via String.format()).

Once you have this down you can also include a temporary boolean that is changed based upon the correct or incorrect value provided by the user. This boolean will then be read on the next iteration of the while loop to then give you the choice of either printing a new question to the screen (via your method) or printing your saved formatted string, which was the previous question.

Comments

0

Add 2 new variable

boolean isTrue = true;
boolean isFalse = true;

And edit this section

if (isTrue)    
    int questionAns = getQuestion(type, difficulty);
int answer = scanner.nextInt();

if (questionAns != answer) {
    System.out.println(getWrongResponse());
    wrongCtr++;
    isTrue = false;
    isFalse = true;
} else {
    System.out.println(getCorrectResponse());
    correctCtr++;
    isTrue = true;
    isFalse = false;
}

1 Comment

Why do you need two?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.