Home > Software engineering >  post increment inside an if statement
post increment inside an if statement

Time:04-26

My output is always 10 even though half of my answers are wrong.

package LabExercise;

import java.util.Scanner;
import java.util.InputMismatchException;

public class LabExer5B {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        try {
            
            String[] Questions = new String[10];
            String[] Choice = new String[10];
            Questions[0] = "1. What is the biggest island in the Philippines?";
            Questions[1] = "2. Jose Rizal's birthday?";
            Questions[2] = "3. Jose Rizal's death anniversary?0";
            Questions[3] = "4. What is the capital of the Philippines?";
            Questions[4] = "5. Festival held in Baguio City every february?";
            Questions[5] = "6. Andres Bonifacio's birthday?";
            Questions[6] = "7. Independence day of Philippines?";
            Questions[7] = "8. How many islands are there in the Philippines?";
            Questions[8] = "9. One of Jose Rizal's famous novel when he was imprisoned?";
            Questions[9] = "10. Writer of Lupang Hinirang?";

            Choice[0] = "A. Luzon \nB. Mindanao \nC. Visayas";
            Choice[1] = "A. June 19 \nB. December 30 \nC. November 2";
            Choice[2] = "A. April 20\nB. December 30 \nC. October 15";
            Choice[3] = "A. Manila City \nB. Taguig City \nC. Quezon City";
            Choice[4] = "A. Mascara Festival \nB. Sinulog \nC. Panagbenga Festival";
            Choice[5] = "A. March 18 \nB. October 28 \nC. November 30";
            Choice[6] = "A. June 12 \nB. October 25 \nC. July 19";
            Choice[7] = "A. 7107 \nB. 7023 \nC. 7192";
            Choice[8] = "A. Noli Me Tangere \nB. Florante at Laura \nC. Ibong Adarna";
            Choice[9] = "A. Sharon Cuneta \nB. Julian Felipe \nC. Rico Puno";
            
          
            String[] Answers = {"A", "A", "B", "A", "C", "C", "A", "A", "A", "B"};
            String[] Letter = new String[10];
            boolean CAnswers = false;
            int score = 0;
         
            int j = 0;
            for(int i = 0; i < 10;i  ){
                 System.out.println(Questions[i]);
                System.out.println(Choice[i]);
                System.out.println("Enter your answer: ");
              
                Letter[j] = input.nextLine();
                if (Letter[j].equalsIgnoreCase(Answers[j])){
                    score  ;
                }  
            }
            System.out.println("Your score is "   score);
        } catch (InputMismatchException e) {
            System.out.println("Please input a letter");
        }
    }
}

I think it is not reading my if statement, do I need to change the if statement?

CodePudding user response:

Why do you have i and j? You only change i, j stays 0 forever and therefore you always compare the first entry of Answers, an "A".

Solution: remove the j and replace where it was used with an i.

Optionally: remove Letter as an array entirely and just have a single String givenAnswer = input.nextLine(); in the loop.

  •  Tags:  
  • java
  • Related