KnowledgeBoat Logo

Computer Applications

Write a program to do the following :

(a) To output the question "Who is the inventor of Java" ?

(b) To accept an answer.

(c) To print out "Good" and then stop, if the answer is correct.

(d) To output the message "try again", if the answer is wrong.

(e) To display the correct answer when the answer is wrong even at the third attempt and stop.

Java

Java String Handling

6 Likes

Answer

import java.util.Scanner;

public class KboatQuiz
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        
        String q = "Who is the inventor of JAVA?";
        String a = "James Gosling";
        int i;
        
        System.out.println(q);
        
        for(i = 1; i <= 3; i++)
        {
            String ans = in.nextLine();
            if(ans.equalsIgnoreCase(a))
            {
                System.out.println("Good");
                break;
            }
            else if(i < 3)
            {
                System.out.println("Try Again");
            }
        }
        
        if(i == 4)
        {
            System.out.println("Correct Answer: " + a);
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to do the following : (a) To output the question "Who is the inventor of Java" ? (b) To accept an answer. (c) To print out "Good" and then stop, if the answer is correct. (d) To output the message "try again", if the answer is wrong. (e) To display the correct answer when the answer is wrong even at the third attempt and stop.BlueJ output of Write a program to do the following : (a) To output the question "Who is the inventor of Java" ? (b) To accept an answer. (c) To print out "Good" and then stop, if the answer is correct. (d) To output the message "try again", if the answer is wrong. (e) To display the correct answer when the answer is wrong even at the third attempt and stop.BlueJ output of Write a program to do the following : (a) To output the question "Who is the inventor of Java" ? (b) To accept an answer. (c) To print out "Good" and then stop, if the answer is correct. (d) To output the message "try again", if the answer is wrong. (e) To display the correct answer when the answer is wrong even at the third attempt and stop.

Answered By

3 Likes


Related Questions