Playing Quiz in Java

import java.util.Scanner;

class PlayQuiz {

         public static void main(String[] arg) {

                     Scanner sc = new Scanner(;

                     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)) {



                                     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 © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site 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 or the content authors.