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.
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
Related Questions
Write a program, which will get text string and count all occurrences of a particular word.
Give the output of the following program :
class MainString { public static void main(String[ ] args) { StringBuffer s = new StringBuffer("String"); if((s.length( ) > 5) && (s.append("Buffer").equals("X"))) ; // empty statement System.out.println(s); } }
Write a program to extract a portion of a character string and print the extracted string. Assume that m characters are extracted, starting with the nth character.
What will be the output for the following program segment?
String s = new String ("abc"); System.out.println(s.toUpperCase( ));