how to have multiple conditions in an if statement java

Solutions on MaxInterview for how to have multiple conditions in an if statement java by the best coders in the world

showing results for - "how to have multiple conditions in an if statement java"
Lucia
04 Nov 2017
1//Program to check if the answer to the given question is correct
2//with multiple conditions in a single else if statement
3import java.util.Scanner;
4public class MultipleChoiceQuestion {
5    public static void main(String args[]) {
6        String question = "How many types of loops exist? ";
7        String choiceOne = "One";
8        String choiceTwo = "Two";
9        String choiceThree = "Three";
10
11        String correctAnswer = choiceThree;
12
13        // Write a print statement asking the question
14        System.out.println(question);
15        // Write a print statement giving the answer choices
16        System.out.println(choiceOne);
17        System.out.println(choiceTwo);
18        System.out.println(choiceThree);
19        // Have the user input an answer
20        Scanner sc = new Scanner(System.in);
21        // Retrieve the user's input
22        String answer = sc.next();
23
24        // If the user's input matches the correctAnswer...
25        // then the user is correct and we want to print out a congrats message to the user.
26        if (answer.equals(choiceThree)) {
27            System.out.println("Congrats! It's the correct answer!");
28        }
29        // If the user's input does not match the correctAnswer...
30        // then the user is incorrect and we want to print out a message saying that the user is incorrect as well as what the correct choice was.
31        else if (answer.equals(choiceOne) || answer.equals(choiceTwo)){
32                System.out.println("Sorry wrong answer. The correct answer is "+choiceThree);
33            }
34        else{
35                System.out.println("Incorrect Input! Check again!");
36            }
37
38}
39}
40
similar questions