Playing Quiz in Java

PlayQuiz.java

import java.util.Scanner;

class PlayQuiz {

         public static void main(String[] arg) {

                     Scanner sc = new Scanner(System.in);

                     String answer = "James Gosling"; 

                     System.out.println("Q. Who is the Inventor of Java? (You have 3 chances to answer correctly)");

                     for (int i = 0; i < 3; i++) {
 
                                     System.out.println("Chance no." + (i + 1));

                                     String guess = sc.nextLine();

                                     if (guess.equalsIgnoreCase(answer)) {

                                                  System.out.println("GOOD!");

                                                  System.exit(0);
                                     }

                                     else if (i<2)

                                                    System.out.println("No! Try again!");

                      } 

                      System.out.println("The correct answer is "+answer);

         }

Java Tips

See also

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.