Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

Program to ask question and accept answer up to 3 attempt and display right answer if it is wrong

Posted By: Easy Tutor     Category: Java     Views: 5232

Write a program to do the following :
a) To output the question "Who is the inventor of C++"?
b) To Accept an answer
c) To print out "Good" and then stop, if the answer is correct.
d) To output 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.

Code for Program to ask question and accept answer up to 3 attempt and display right answer if it is wrong in Java

import java.io.*;

class  Inventor
{
    publicstaticvoid main(String args[]) 
    {
       final String ans="BJARNE STROUSTRUP";
       String str;
       int n=3,attempt=0;

       try{
          BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));

          while(n>0)
          {
              System.out.print("Who is Founder of C++ : ");
              System.out.flush();
              str=obj.readLine();
              attempt++;
              if( str.equalsIgnoreCase(ans) == true)
              {
                     if(attempt==1)
                     {
                         System.out.println("\nVERY GOOD");
                         System.exit(1);
                     }
                     elseif(attempt==2)
                      {
                         System.out.println("\nGOOD");
                         System.exit(1);
                      }
                     else
                      {
                         System.out.println("CORRECT");
                         System.exit(1);
                      }
            }
            else
                   System.out.println("\nTRY AGAIN\n");
            
            n=n-1;
         }
         if(attempt>=3)
            System.out.println("\nANSWER IS "+ans);
        }
    catch(Exception e) {}
    }
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to ask question and accept answer up to 3 attempt and display right answer if it is wrong is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!