Home > Mobile >  My program executes the else statements even though it should stop on the if statement
My program executes the else statements even though it should stop on the if statement

Time:06-18

My program proceeds to the else statement even though the user inputs the correct answer and the if statement is run.

package dowhileloops;

import java.util.Scanner;

public class DoWhileLoops {

    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        String res, ans;
        
        System.out.println("=========================================");
        System.out.println("     ****     Do-While Loops     ****    ");
        System.out.println("=========================================\n");
        
        do{
            System.out.println("What is the color of the ocean?");
            System.out.println("\nA.) Orange\n\nB.) Green\n\nC.) Blue\n\nD.) Yellow\n\n");
            System.out.print("Enter your answer: ");
            res = in.nextLine();

            if (res.equalsIgnoreCase("C"))
                System.out.println("You are correct!");
                else
                    System.out.println("You are wrong!");
                        System.out.println("Would you like to try again? (Yes/No)");
                        ans = in.nextLine();
               
        }while(ans.equalsIgnoreCase("Yes"));        
    }
    
}

It proceeds to execute the line System.out.println("Would you like to try again? (Yes/No)"); ans = in.nextLine(); even though it should stop at System.out.println("You are correct!");

I want the program to terminate after the user types the correct answer "C"

CodePudding user response:

You should accept the char not the string.

res = in.nextLine().charAt(0);

this will accept the first char no matter the rest of the chars.

CodePudding user response:

It is happening because you did not put curly brackest { and }.

Here is the code with the curly brackets.

package dowhileloops;
import java.util.Scanner;

public class DoWhileLoops {

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

        System.out.println("=========================================");
        System.out.println("     ****     Do-While Loops     ****    ");
        System.out.println("=========================================\n");

        do {
            System.out.println("What is the color of the ocean?");
            System.out.println("\nA.) Orange\n\nB.) Green\n\nC.) Blue\n\nD.) Yellow\n\n");
            System.out.print("Enter your answer: ");
            res = in.nextLine();

            if (res.equalsIgnoreCase("C"))
                System.out.println("You are correct!");
            else {
                System.out.println("You are wrong!");
                System.out.println("Would you like to try again? (Yes/No)");
                ans = in.nextLine();
            }

        } while (ans.equalsIgnoreCase("Yes"));
    }
}
  • Related