mercredi 3 novembre 2021

How can I make the answer of the previous question become the first operand of the next question?

               switch(choice) {
                case 1:
                 System.out.println("1) keep counting game");
                 
                       
                    System.out.println("Keep Counting");
                   
                    System.out.println("--------------");
                     
                    System.out.println("You will be presented with 10 addition questions.");
                    System.out.println("After the first question, the left-hand operand");
                    System.out.println("is the result of the previous addition.");
                     Scanner enter = new Scanner(System.in);
                    System.out.println("Press enter to start...");
                       enter.nextLine();                               //allows the Enter key to be pressed to start game
              
         final int NUMBER_OF_QUESTIONS = 10;    // Number of questions
    int correctCount = 0;   // Count the nubmer of correct answers
    int count = 0;  // Count the number of questions
    long startTime = System.currentTimeMillis();
    String output = " "; // output string is initially empty
    Scanner input = new Scanner(System.in);

    while (count < NUMBER_OF_QUESTIONS) {
        //  Generate two random single-digit integers
        int number1 = 1 + (int)(Math.random() * 10);
        int number2 = 1 + (int)(Math.random() * 10);

        //  asks "What is number1 + number2?"
        System.out.print(
            "What is " + number1 + " + " + number2 + "? ");
        int answer = input.nextInt();

        //tells us if the answer is correct or not 
        if (number1 + number2 == answer) {
            System.out.println("You are correct!");
            correctCount++; // Increase the correct answer count
            
        }   
        else if (number1 + number2 == answer){
            number1 = answer;
        }
        else
            System.out.println("Your answer is wrong.\n" + number1
                + " + " + number2 + " should be " + (number1 + number2));
        
        // Increase the question count
        count++;

        output += "\n" + number1 + "+" + number2 + "=" + answer +
        ((number1 + number2 == answer) ? " correct" : " wrong");
        
        if (number1 + number2 != answer){        //game ends if user gets question wrong
            System.out.println("Your answer is wrong.\n" + number1
                + " + " + number2 + " should be " + (number1 + number2));
            break;
        }
    }
               
    long endTime = System.currentTimeMillis();
    long testTime = endTime - startTime; // end the timer

    System.out.println("Correct count is " + correctCount + //shows us the correct             amount of answers 
        "\nTest time is " + testTime / 1000 + " seconds\n" + output);  //shows us    the amount of time taken to complete quiz
              break;                                                    
                  

I have put in an else if statement which said if the answer is correct, the first operand (number1 will be equal to the previous answer) but It didn't seem to work.maybe I have done this else if statement completely wrong or is there another way to implement this into my code? thanks for the help :)

Aucun commentaire:

Enregistrer un commentaire