Write JAVA program to do the following:
i. To output the question “who is the inventor of java”?
ii. To accept an answer
iii. To print out “Good” and then stop, if the answer is correct.
iv. To output the message “try again”, if the answer is wrong.
v. To display the correct answer when the answer is wrong even at the third attempt.
Answers
Explanation:
import java.util.Scanner;
class prog
{
void main()
{int i=0;
Scanner sc=new Scanner(System.in);
System.out.println("Who is the inventor of JAVA?");
for(i=1;i<=3;i++)
{
String a=sc.nextLine();
if(a.equalsIgnoreCase("James Gosling"))
{System.out.println("Good");
break;}
else
{System.out.println("Try Again");
}
}
if(i==4)
{
System.out.println("James Gosling is the inventor of JAVA");
}
}
}
Answer:
import java.util.Scanner;
class prog
{
void main()
{int i=0;
Scanner sc=new Scanner(System.in);
System.out.println("Who is the inventor of JAVA?");
for(i=1;i<=3;i++)
{
String a=sc.nextLine();
if(a.equalsIgnoreCase("James Gosling"))
{System.out.println("Good");
break;}
else
{System.out.println("Try Again");
}
}
if(i==4)
{
System.out.println("James Gosling is the inventor of JAVA");
}
}
}