1 Replies - 781 Views - Last Post: 25 November 2015 - 03:29 AM Rate Topic: -----

#1 IsabellerRiver   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-November 15

Need Help with JAVA Quiz

Posted 24 November 2015 - 07:53 PM

Hi guys,

i'm a beginner in programming and I'm currently working on a multiple choice quiz.

Both options branch out into 3 separate quizzes.
(Example, User chooses "Riddles" --> option 1: Animals , option 2: Logic , option 3: Objects)

At the end of these quizzes, the user is given the options to return to main menu (to choose a different Subject) or the option to return to the Subject menu to pick another quiz and the final option is to quit altogether.

I am aware that I am not using arrays and only methods, everything runs fine, but I would like to add another feature to the program.

How do I code the program in a way where the user can exit the program without having to complete all of the questions in the current quiz they're in?

/* A Java application using methods that executes different categories of quizzes according to 
*  user choice.
*/

import java.util.Scanner; // import scanner

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class QuizAttempt1 { // class	
	public static void main (String []args) {
		
		Scanner input = new Scanner (System.in);
		// print welcome and main menu 
		System.out.println("Welcome to The Mega Quiz");
		System.out.println();
		System.out.println("Subjects of Quiz");
		System.out.println();
		System.out.println("Science = 1"); // option 1
		System.out.println("Riddles = 2"); // option 2
		System.out.println();
		        System.out.println("Hit 0 to Quit");
		System.out.println();
		
		boolean quit = false ;
	int subject;
	do {
		System.out.println();
		System.out.println();
		System.out.println("Choose a subject : "); // prompt user to choose a subject 
		subject = input.nextInt();
		switch (subject){ // start switch 
			case 1: // option to choose science quizzes
				System.out.println();
				System.out.println("You've choosen Science");
				science(); // program will execute science method 
				break;
			case 2: // option to choose riddle quizzes
				System.out.println();
				System.out.println("You've choosen Riddles");
				riddles(); // program will execute riddles method
				break;
			case 0: // option to quit
				quit = true;
				break;
			default:
				System.out.println("Invalid choice. Please try again."); 
		} // end of switch 
	} while (!quit);
	  System.out.println();
	System.out.println("Thank you! Good-bye!");
	
	}

		public static void science() { // start science method
			
			Scanner input = new Scanner (System.in);
			// Science main menu 
			System.out.println("Welcome to Science");
			System.out.println();
			System.out.println("Categories of Science");
			System.out.println();
			System.out.println("Senses = 1"); // option 1
			System.out.println("Plants = 2"); // option 2
			System.out.println("Magnet = 3"); // option 3
			System.out.println();
			System.out.println("Back to Main Menu = 4 "); // option 4 
			System.out.println("Hit 0 to Quit"); // Quit option 
			        
			        
			        
			System.out.println();
			
			
			boolean quit = false;
		int subjectCategory;
		do{
			System.out.println();
			System.out.println();
		System.out.print("Choose subject category : "); // prompt user to choose subject category
		subjectCategory = input.nextInt();
		switch (subjectCategory) { // start switch 
			case 1: // option to choose Senses quiz
				System.out.println();
				System.out.println("You've choosen Senses");
				Senses(); // program will execute method Senses 
				break;
			case 2: // option to choose Plants quiz
				System.out.println();
				System.out.println("You've choosen Plants");
				Plants(); // program will execute method Plants
				break;
			case 3: // option to choose Magnet quiz
				System.out.println();
				System.out.println("You've choosen Magnet");
				Magnet(); // program will execute method Magnet
				break;
			case 4: // option to go back to main menu 
				System.out.println();
				System.out.println("Main Menu"); 
				main(null); // program will bring user back to main menu 
			case 0: // option to quit 
				quit = true;
				break;
			default:
				System.out.println("Invalid choice. Please try again");
		     } //end switch 
	  } while (!quit);
	    System.out.println();
	  System.out.println("Thank you! Good-bye!");
		} // end science method 
		
		public static void riddles() { // start riddles method 
			
			Scanner input = new Scanner (System.in); // create Scanner input
			
			// Riddles main menu 
			System.out.println("Welcome to Riddles 101"); 
			System.out.println();
			System.out.println("Categories of Riddles"); 
			System.out.println();
			System.out.println("Animals = 1"); // option 1
			System.out.println("Objects = 2");// option 2 
			System.out.println("Logic = 3"); // option 3
			System.out.println();
			System.out.println("Back to Main Menu = 4 "); // option 4 
			System.out.println("Hit 0 to Quit."); // Quit option 
			
			
			System.out.println();
			
			
			boolean quit = false;
	        int subjectCategory;
	        do {
	        	 System.out.println();
	        	 System.out.println();
	              System.out.print("Choose subject category : ");
	              subjectCategory = input.nextInt();
	              switch (subjectCategory) { // start switch 
	              case 1: // option to choose Animals quiz 
	            	  System.out.println();
	                    System.out.println("You've chosen Animals");
	                    Animals();// Program will execute Animal related riddles
	                    break;
	              case 2: // option to choose Objects quiz 
	            	  System.out.println();
	                    System.out.println("You've chosen Objects");
	                    Objects();// Program will execute Object related riddles
	                    break;
	              case 3: // option to choose Logics quiz 
	            	  System.out.println();
	                    System.out.println("You've chosen Logics");
	                    Logic();// Program will execute Logic related riddles
	                    break;
	              case 4: // option to go back to main menu 
	  				System.out.println();
	  				System.out.println("Main Menu ");
	  				main(null); // program will bring user back to main menu 
	  				break;
	              case 0: // option to quit 
	                  quit = true;
	                  break;
	            default:
	                  System.out.println("Invalid choice. Please try again.");
	            } // end switch 
	      } while (!quit);
	        System.out.println();
	      System.out.println("Thank you! Good-bye!");
		} // end riddles method 
		
		public static void Senses() {
			int c = 0;
			String in;
			float percentage;
			Scanner input = new Scanner (System.in); // Scanner for answer 
		    
		    // Title
		    System.out.println("Welcome to Science about Senses!");
		    System.out.println();
		    // Instruction for Quiz
		    System.out.println("Instruction");
		    System.out.println("1. Please answer all the question provided.");
		    System.out.println("2. Submit your answer by using the Enter key on your keyboard.");
		    System.out.println();
		    
		    //Question 1
		    System.out.println("1.How many types of senses do humans have?");
		    System.out.println("a) Six");
		    System.out.println("B)/>/> Three");
		    System.out.println("c) Five");
		    
		 System.out.print("Answer:");
		    in = input.nextLine();
		    
		    if(in.equalsIgnoreCase("c"))
		    	
		          {
		          	System.out.println("Correct! Good Work!");
		          	      System.out.println();
		          	      
		          	c++;
		          }
		          else
		          {
		          	System.out.println("Incorrect");
		          	      System.out.println();
		          	      		          	      
		          }
		    
		    		
		    
		    // Question 2
		    System.out.println("2.Which item gives out bad smell?");
		    System.out.println("a) Rubbish");
		    System.out.println("B)/>/> Rose");
		    System.out.println("c) Dead Rat");
		    
		    System.out.print("Answer:");
		    in = input.nextLine();
		    
		    if (in.equalsIgnoreCase("b"))
		    {
		    	System.out.println("Correct! Good Job!");
		    	System.out.println();
		    	c++;
		    } 
	        else
	        { 
	            System.out.println("Incorrect");
	            System.out.println();
	           
	        }
	        
	     // Question 3
	            System.out.println("3.Which food tastes bitter?");
	            System.out.println("a) Mango");
	            System.out.println("B)/>/> Coffee");
	            System.out.println("c) Chocolate");
	            
	            System.out.print("Answer:");
	            in = input.nextLine(
	            );
	            if(in.equalsIgnoreCase("b"))
	            	
	            {
	            	System.out.println("Correct! Excellent!");
	            	System.out.println();
	            	c++;
	            }
	            else
	            {
	            	System.out.println("Incorrect");
	            	System.out.println();
	            	
	            }
	            
	            
	            // Question 4
	            System.out.println("4.The boy is ________ the guitar.");
	            System.out.println("a) Plucking");
	            System.out.println("B)/>/> Shaking");
	            System.out.println("c) Blowing");
	            
	            System.out.print("Answer:");
	            in = input.nextLine();
	            
	            if (in.equalsIgnoreCase("a"))
	            {
	            	System.out.println("Correct! Well Done!");
	            	System.out.println();
	            	c++;
	            }
	            else 
	            {
	            	System.out.println("Incorrect");
	            	System.out.println();
	            	
	            }
	            
	            
	            // Question 5 
	            System.out.println("5.What does a lion do?");
	            System.out.println("a) Meow");
	            System.out.println("B)/>/> Cluck");
	            System.out.println("c) Roar");
	            
	            System.out.print("Answer:");
	            in = input.nextLine();
	            
	            if (in.equalsIgnoreCase("c"))
	            {
	            	System.out.println("Correct! You Rock!");
	            	System.out.println();
	            	c++;
	            
	            }
	            else
	            {
	            	System.out.println("Incorrect");
	            	System.out.println();
	            	
	            }
	            // end of Questions
	            
	            //calculate percentage
	            percentage = (c * 100/5);
	            
	            //Display Quiz Result
	            System.out.println("Result:");
	            System.out.println(c + " Correct out of 5");
	            System.out.println("Your final percentage is " + percentage + "%");	            
	            backoptionscience(); // Option to return to main menu or science menu 
	           
	            
		}
		
		public static void Plants() { 
			
			Scanner input = new Scanner (System.in); 
			
		     int c = 0; 
		     String in; 
		     float percentage;
		
		     // Title 
		     System.out.println("Welcome to Science about Plants!");
		     System.out.println();
		     // Instructions for quiz
		     System.out.println("Instruction");
		     System.out.println("1. Please answer all the questions provided. ");
		     System.out.println("2. Submit your answer by using the Enter key on your keyboard." );
		     System.out.println();
		     
		     
		     // Question 1
		     System.out.println("1.Which plant has oval shaped leaves?");
		     //options for Question 1
		     System.out.println("a) Rose plant");
		     System.out.println("B)/>/> Balsam plant");
		     System.out.println("c) Cactus");
		     
		     System.out.print("Answer:"); 
		         in = input.nextLine();
		       
		         if (in.equalsIgnoreCase("a"))
		         
		         {
		         	System.out.println("Correct! Great Job!"); 
		         	       System.out.println();
		         }
		         
		         else
				 {
				 	System.out.println("Incorrect."); 
				 	System.out.println();
				 	
				 	
		 // end of Question 1
		 
			 // Question 2
			 System.out.println("2.Which of the following plants is non-flowering?");
			 //options for Question 2
			 System.out.println("a) Fern");
			 System.out.println("B)/>/> Balsam plant");
			 System.out.println("c) Hibiscus plant");
			 
			 System.out.print("Answer:");
			 in = input.nextLine();
			 
			
			 if (in.equalsIgnoreCase("a"))
			 
			 {
			 	System.out.println("Correct! Excellent!");
			 	System.out.println();
			 	c++;
			 	
			 }
			 else
			 {
			 	System.out.println("Incorrect."); 
			 	System.out.println();
			 	
			 	
			 }
			 //end of Question 2
			 
			 // Question 3
			 System.out.println("3.Which plant do have woody stems?");
			 //options for Question 3
			 System.out.println("a) Mimosa");
			 System.out.println("B)/>/> Banana tree");
			 System.out.println("c) Rubber tree");
			 
			 System.out.print("Answer:"); 
			 in = input.nextLine();
			 
			 
			 if (in.equalsIgnoreCase("c")) 
			 
			 {
			 	System.out.println("Correct! Good thinking!");
			 	System.out.println();
			 	c++;
			 }
			 else
			 {
			 	System.out.println("Incorrect."); 
			 	System.out.println();
			 	
			 }
			 //end of Question 3
			 
			 
			 //Question 4
			 System.out.println("4.Which plant is grown by seed planting?");
			 //options for Question 4
			 System.out.println("a) Coconut tree");
			 System.out.println("B)/>/> Hibiscus plant");
			 System.out.println("c) Fern");
			 
			 System.out.print("Answer:");
			 in = input.nextLine();
			 
			 
			 if (in.equalsIgnoreCase("a")) 
			 
			 {
			 	System.out.println("Correct! Good thinking!");
			 	System.out.println();
			 	c++;
			 }
			 else
			 {
			 	System.out.println("Incorrect."); 
			 	System.out.println();
			 	
			 }
			 // end of Question 4
			 
			 //Question 5
			 System.out.println("5.Which leaves of the following leaves have irregular shapes:");
			 //options for Question 5
			 System.out.println("a) Pandanus leaf");
			 System.out.println("B)/>/> Papaya leaf");
			 System.out.println("c) Rose Leaf");
			 
			 System.out.print("Answer:"); 
			 in = input.nextLine();
			 
			 
			 if (in.equalsIgnoreCase("b"))
			 
			 {
			 	System.out.println("Correct! Wow, amazing!");
			 	System.out.println();
			 	c++;
			 }
			 else
			 {
			 	System.out.println("Incorrect."); 
			 	System.out.println();
			 	
			 }
			 //end of Question 5
		 
		
		 percentage = (c*100/5);
		 

		 System.out.println("Result:");
		 System.out.println(c + " Correct out of 5");
		 System.out.println("Your final percentage is " + percentage + "%");
		 backoptionscience(); // option to return to main menu or science menu 
		}
		}
		 
		
		
		public static void Magnet() { 
			
			Scanner input = new Scanner (System.in);
			   
			int c = 0;
			String in; 
	        float percentage;
		  
		    // Title 
		     System.out.println("Welcome to Science about Magnets!");
		     System.out.println();
		     // Instructions for quiz 
		     System.out.println("Instruction");
		     System.out.println("1. Please answer all the questions provided. ");
		     System.out.println("2. Submit your answer by using the Enter botton on your keyboard." );
		     System.out.println();
		     

		     // Question 1
		     System.out.println("1.How many types of magnets do we have?");
		     //options for Question 1
		     System.out.println("a) 9");
		     System.out.println("B)/>/> 6");
		     System.out.println("c) 5");
		     
		     System.out.print("Answer:"); 
		         in = input.nextLine();
		        
		         if (in.equalsIgnoreCase("b"))
		         
		         {
		         	System.out.println("Correct! Great Job!");
		         	 System.out.println();
		         }
		         
		    	 else
				 {
				 	System.out.println("Incorrect."); 
				 	System.out.println();
				 	
				 }
		         // end Question 1
		         
				 // Question 2
				 System.out.println("2.Which magnet pole combination attracts?");
				 //options for Question 2
				 System.out.println("a) North and South");
				 System.out.println("B)/>/> South and South");
				 System.out.println("c) North and North");
				 
				 System.out.print("Answer:");
				 in = input.nextLine();
				 
				 
				 if (in.equalsIgnoreCase("a"))
				 
				 {
				 	System.out.println("Correct! Excellent!"); 
				 	System.out.println();
				 	c++;
				 	
				 }
				 else
				 {
				 	System.out.println("Incorrect."); 
				 	System.out.println();
				 	
				 }
				 // end of Question 2
				 
				 // Question 3
				 System.out.println("3.Which object will attracted by a magnet?");
				 //options for Question 3
				 System.out.println("a) Iron spoon");
				 System.out.println("B)/>/> Matches");
				 System.out.println("c) Photo frame");
				 
				 System.out.print("Answer:"); 
				 in = input.nextLine();
				
				 if (in.equalsIgnoreCase("a"))
				 {
				 	System.out.println("Correct! Good thinking!");
				 	System.out.println();
				 	c++;
				 }
				 else
				 {
				 	System.out.println("Incorret.");
				 	System.out.println();
				 	
				 }
				 //end of Question 3
				 
				 
				 //Question 4
				 System.out.println("4.Which of the following objects requires a magnet?");
				 //options for Question 4
				 System.out.println("a) Bottle");
				 System.out.println("B)/>/> Compass");
				 System.out.println("c) Watch");
				 
				 System.out.print("Answer:");
				 in = input.nextLine();
				 
				 
				 if (in.equalsIgnoreCase("b")) 
				 
				 {
				 	System.out.println("Correct! Good thinking!");
				 	c++;
				 }
				 else
				 {
				 	System.out.println("Incorrect."); 
				 	System.out.println();
				 	
				 }
				 // end of Question 4
				 
				 //Question 5
				 System.out.println("5.Which of the following magnets is the strongest magnet?:");
				 //options for Question 5
				 System.out.println("a) Attract 5 paper clips");
				 System.out.println("B)/>/> Attract 18 paper clips");
				 System.out.println("c) Attract 10 paper clips");
				 
				 System.out.print("Answer:"); 
				 in = input.nextLine();
				 
				 
				 if (in.equalsIgnoreCase("b")) 
				 {
				 	System.out.println("Correct! Wow, amazing!");
				 	System.out.println();
				 	c++;
				 }
				 else
				 {
				 	System.out.println("Incorrect."); 
				 	System.out.println();
				 	
				 }
				 //end of Question 5
		 
		
		 percentage = (c*100/5);
		 
		 
		 System.out.println("Result:");
		 System.out.println(c + " Correct out of 5");
		 System.out.println("Your final percentage is " + percentage + "%");
		 backoptionscience(); // option to return to main menu or science menu 
		}
		
		public static void Animals() {
			
			int c = 0; // Declare and initialize variable c
			String in; //declare variable in
		    float percentage;
			
			Scanner input = new Scanner (System.in); // create scanner
			
			// Title
			System.out.println("Welcome to Riddles about Animals!");
			System.out.println();
			//Instructions for Quiz 
			System.out.println("Instructions");
			System.out.println("1. Please answer all the questions provided. ");	
			System.out.println("2. Submit your answer by using the Enter button on your keyboard." );
			System.out.println();
		
			
			// Question 1
			System.out.println("1.It’s at home in a caravan traveling across the sand."
					+ " Its back is peculiar, but it’s a lifesaver in a hostile land. "
					+ "What is it?");
			//options for Question 1
			System.out.println("a) Camel");
			System.out.println("B)/>/> Bear");
			System.out.println("c) Snake");
			System.out.println("d) Bunny");
			
		    System.out.print("Answer:"); // prompt user to answer 
			in = input.nextLine();
			// start if for Question 1
			if (in.equalsIgnoreCase("a")) // condition / answer for Question 1
			
				{
				 System.out.println("Correct! Good Job!"); // if answer is correct, system will print "correct"
					System.out.println();
				 c++;
				}
				else
				{
				System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
					System.out.println();
					
				}
			// end of Question 1

			// Question 2
			System.out.println("2.What kind of dog chases anything red?");
			//options for Question 2
			System.out.println("a) Pomeranian");
			System.out.println("B)/>/> Labrador Retriever");
			System.out.println("c) Bulldog");
			System.out.println("d) Chihuahua");
			
			 System.out.print("Answer:"); // prompt user to answer 
			 in = input.nextLine();
			 
			//start if for Question 2
			if (in.equalsIgnoreCase("c")) // condition / answer for Question 2
				
			{
			 System.out.println("Correct! Well done!"); // if answer is correct, system will print "correct"
			 System.out.println();
			 c++;
			}
			else
			{
			System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
			System.out.println();
			
			
			}
			//end of Question 2
			
	        // Question 3
			System.out.println("3.Agile on its feet, it drives dogs mad. It flicks its tail when angry and purrs when glad.What is it?");
			//options for Question 3
			System.out.println("a) Bunny");
			System.out.println("B)/>/> Cat");
			System.out.println("c) Dog");
			System.out.println("d) Frog");
			
			 System.out.print("Answer:"); // prompt user to answer 
			 in = input.nextLine();
			 
			//start if for Question 3
			if (in.equalsIgnoreCase("b")) // condition / answer for Question 3
				
			{
			 System.out.println("Correct! Excellent!");// if answer is correct, system will print "correct"
			 System.out.println();
			 c++;
			}
			else
			{
			System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
			System.out.println();
			
			}
			//end of Question 3
			
			
			//Question 4
			System.out.println("4.What animal is always in trouble with its teachers?");
			//options for Question 4
			System.out.println("a) Tiger");
			System.out.println("B)/>/> Elephant");
			System.out.println("c) Cheetah");
			System.out.println("d) Fish");
			
			 System.out.print("Answer:"); // prompt user to answer 
			 in = input.nextLine();
			 
			//start if for Question 4
			if (in.equalsIgnoreCase("c")) // condition / answer for Question 4
				
			{
			 System.out.println("Correct! Good job!");// if answer is correct, system will print "correct"
			 System.out.println();
			 c++;
			}
			else
			{
			System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
			System.out.println();
			
			}
			// end of Question 4
			
			//Question 5
			System.out.println("5.A creature of the night lacking traditional sight. What is it? ");
			//options for Question 5
			System.out.println("a) Frog");
			System.out.println("B)/>/> Dog");
			System.out.println("c) Reindeer");
			System.out.println("d) Bat");
			
			 System.out.print("Answer:"); // prompt user to answer 
			 in = input.nextLine();
			 
			//start if for Question 5
			if (in.equalsIgnoreCase("d")) // condition / answer for Question 5
				
			{
			 System.out.println("Correct! Wow!");// if answer is correct, system will print "correct"
			 System.out.println();
			 c++;
			}
			else
			{
			System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
			System.out.println();
			
			}
			// end of Question 
			
			// calculate percentage
			percentage = (c * 100/ 5);
			
			// Display Quiz Result
			System.out.println("Result:");
			System.out.println(c +  " Correct out of 5"); // displays how many correct answers out of total answers
	        System.out.println("Your final percentage is " + percentage + "%");
	        backoptionriddles(); // option to return to main menu or riddles menu 
		}
		
		public static void Objects() {
			
			int c = 0; // Declare and initialize variable c
			String in; //declare variable in
		    float percentage;
			
			Scanner input = new Scanner (System.in); // create scanner
			
			// Title
			System.out.println("Welcome to Riddles about Objects!");
			System.out.println();
			//Instructions for Quiz 
			System.out.println("Instructions");
			System.out.println("1. Please answer all the questions provided. ");	
			System.out.println("2. Submit your answer by using the Enter button on your keyboard." );
			System.out.println();
		
			
			// Question 1
			System.out.println("1.What gets wetter and wetter the more it dries?");
			//options for Question 1
			System.out.println("a) Bed");
			System.out.println("B)/>/> Towel");
			System.out.println("c) Raincoat");
			System.out.println("d) Cup");
			
		    System.out.print("Answer:"); // prompt user to answer 
			in = input.nextLine();
			// start if for Question 1
			if (in.equalsIgnoreCase("b")) // condition / answer for Question 1
			
				{
				 System.out.println("Correct! Great Job!"); // if answer is correct, system will print "correct"
					System.out.println();
				 c++;
				}
				else
				{
				System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
					System.out.println();
					
				}
			// end of Question 1

			// Question 2
			System.out.println("2.What kind of tree can you carry in your hand?");
			//options for Question 2
			System.out.println("a) Oak Tree");
			System.out.println("B)/>/> Coconut Tree");
			System.out.println("c) Palm Tree");
			System.out.println("d) Mango Tree");
			
			 System.out.print("Answer:"); // prompt user to answer 
			 in = input.nextLine();
			 
			//start if for Question 2
			if (in.equalsIgnoreCase("c")) // condition / answer for Question 2
				
			{
			 System.out.println("Correct! Excellent!"); // if answer is correct, system will print "correct"
			 System.out.println();
			 c++;
			}
			else
			{
			System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
			System.out.println();
			
			}
			//end of Question 2
			
	        // Question 3
			System.out.println("3.What can travel around the world while staying in a corner?");
			//options for Question 3
			System.out.println("a) Stamp");
			System.out.println("B)/>/> Airplane");
			System.out.println("c) Helicopter");
			System.out.println("d) Pencil");
			
			 System.out.print("Answer:"); // prompt user to answer 
			 in = input.nextLine();
			 
			//start if for Question 3
			if (in.equalsIgnoreCase("a")) // condition / answer for Question 3
				
			{
			 System.out.println("Correct! Excellent!");// if answer is correct, system will print "correct"
			 System.out.println();
			 c++;
			}
			else
			{
			System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
			System.out.println();	
			
			}
			//end of Question 3
			
			
			//Question 4
			System.out.println("4.What room can no one enter?");
			//options for Question 4
			System.out.println("a) Supply Room");
			System.out.println("B)/>/> Mushroom");
			System.out.println("c) Bedroom");
			System.out.println("d) Personal Office");
			
			 System.out.print("Answer:"); // prompt user to answer 
			 in = input.nextLine();
			 
			//start if for Question 4
			if (in.equalsIgnoreCase("b")) // condition / answer for Question 4
				
			{
			 System.out.println("Correct! Good thinking!");// if answer is correct, system will print "correct"
			 System.out.println();
			 c++;
			}
			else
			{
			System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
			System.out.println();
			
			}
			// end of Question 4
			
			//Question 5
			System.out.println("5.What has one eye but cannot see? ");
			//options for Question 5
			System.out.println("a) Glasses");
			System.out.println("B)/>/> Clock");
			System.out.println("c) Needle");
			System.out.println("d) Wristwatch");
			
			 System.out.print("Answer:"); // prompt user to answer 
			 in = input.nextLine();
			 
			//start if for Question 5
			if (in.equalsIgnoreCase("c")) // condition / answer for Question 5
				
			{
			 System.out.println("Correct! Wow, amazing!");// if answer is correct, system will print "correct"
			 System.out.println();
			 c++;
			}
			else
			{
			System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
			System.out.println();
			
			}
			// end of Question 
			
			// calculate percentage
			percentage = (c * 100/ 5);
			
			// Display Quiz Result
			System.out.println("Result:");
			System.out.println(c +  " Correct out of 5"); // displays how many correct answers out of total answers
	        System.out.println("Your final percentage is " + percentage + "%");
	        backoptionriddles(); // option to return to main menu or riddles menu 
		}
		
		public static void Logic() {
			
			int c = 0; // Declare and initialize variable c
			String in; //declare variable in
		    float percentage;
			
			Scanner input = new Scanner (System.in); // create scanner
			
			// Title
			System.out.println("Welcome to Logic Riddles!");
			System.out.println();
			//Instructions for Quiz 
			System.out.println("Instructions");
			System.out.println("1. Please answer all the questions provided. ");	
			System.out.println("2. Submit your answer by using the Enter button on your keyboard." );
			System.out.println();
		
			
			// Question 1
			System.out.println("1.What is it that's always coming but never arrives?");	
			
		    System.out.print("Answer:"); // prompt user to answer 
			in = input.nextLine();
			// start if for Question 1
			if (in.equalsIgnoreCase("Tomorrow")) // condition / answer for Question 1
			
				{
				 System.out.println("Correct! Excellent!"); // if answer is correct, system will print "correct"
					System.out.println();
				 c++;
				}
				else
				{
				System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
					System.out.println();
					
				}
			// end of Question 1

			// Question 2
		System.out.println("2.Paul's height is six feet, he's an assistant at a butcher's shop, and wears size 9 shoes. What does he weigh?");	
			
		    System.out.print("Answer:"); // prompt user to answer 
			in = input.nextLine();
			// start if for Question 2
			if (in.equalsIgnoreCase("Meat")) // condition / answer for Question 2
			
				{
				 System.out.println("Correct! You got it!"); // if answer is correct, system will print "correct"
					System.out.println();
				 c++;
				}
				else
				{
				System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
					System.out.println();
				
				}
			//end of Question 2
			
	        // Question 3
		System.out.println("3.Which word in the dictionary is spelled incorrectly?");	
			
		    System.out.print("Answer:"); // prompt user to answer 
			in = input.nextLine();
			// start if for Question 3
			if (in.equalsIgnoreCase("Incorrectly")) // condition / answer for Question 3
			
				{
				 System.out.println("Correct! Great Job!"); // if answer is correct, system will print "correct"
					System.out.println();
				 c++;
				}
				else
				{
				System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
					System.out.println();
					
				}
			//end of Question 3
			
			
			//Question 4
		System.out.println("4.If you have me, you want to share me. If you share me, you haven't got me. What am I?");	
			
		    System.out.print("Answer:"); // prompt user to answer 
			in = input.nextLine();
			// start if for Question 4
			if (in.equalsIgnoreCase("Secret")||(in.equalsIgnoreCase("a secret")||(in.equalsIgnoreCase("secrets"))))// condition / answer for Question 4
			 
				{
				 System.out.println("Correct! Woo-hoo!"); // if answer is correct, system will print "correct"
					System.out.println();
				 c++;
				}
				else
				{
				System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
					System.out.println();
					
				}
			// end of Question 4
			
			//Question 5
	System.out.println("5.What gets broken without being held?");	
			
		    System.out.print("Answer:"); // prompt user to answer 
			in = input.nextLine();
			// start if for Question 5
			if (in.equalsIgnoreCase("Promise")||(in.equalsIgnoreCase("a promise")||(in.equalsIgnoreCase("promises")))) // condition / answer for Question 4
			
				{
				 System.out.println("Correct! Nice!"); // if answer is correct, system will print "correct"
					System.out.println();
				 c++;
				}
				else
				{
				System.out.println("Incorrect."); // if answer is wrong, system will print "incorrect"
					System.out.println();
					
				}
			// end of Question 
			
			// calculate percentage
			percentage = (c * 100/ 5);
			
			// Display Quiz Result
			System.out.println("Result:");
			System.out.println(c +  " Correct out of 5"); // displays how many correct answers out of total answers
	        System.out.println("Your final percentage is " + percentage + "%");
	        backoptionriddles(); // option to return to main menu or riddles menu 
	       }
			
		public static void backoptionscience() { // back option method for science quizzes
			
			
	    	int in;
			
		    Scanner backoption = new Scanner (System.in);
		    
		    System.out.println("Go back to main menu = 1");
		    System.out.println("Go back to subject menu = 2");
		    System.out.println("Response:");
		    System.out.println();
		    
		    in = backoption.nextInt();
		    
		    switch (in) {
		    case 1: 
		    	System.out.println("Main Menu"); 		   
		    	main(null);
		    	break;
		    case 2: 
		    	System.out.println("Subject Menu");
		    	science();
		    	break;
		    case 0: 
		    	System.out.println("Exit");
		    	boolean quit = true;
		    	break;
		    default:
				System.out.println("Invalid choice. Please try again."); 
		    }
		 boolean quit = false;
		while (!quit);
		  System.out.println();
	
	}
public static void backoptionriddles() { // back option method for riddles quizzes
			
			
	    	int in;
			
		    Scanner backoption = new Scanner (System.in);
		    
		    System.out.println("Go back to main menu = 1");
		    System.out.println("Go back to subject menu = 2");
		    System.out.println("Response:");
		    System.out.println();
		    
		    in = backoption.nextInt();
		    
		    switch (in) {
		    case 1: 
		    	System.out.println("Main Menu"); 		   
		    	main(null);
		    	break;
		    case 2: 
		    	System.out.println("Subject Menu");
		    	riddles();
		    	break;
		    case 0: 
		    	System.out.println("Exit");
		    	boolean quit = true;
		    	break;
		    default:
				System.out.println("Invalid choice. Please try again."); 
		    }
		 boolean quit = false;
		while (!quit);
		  System.out.println();
		
	}
		
}
		



If any of you have any ideas, please advise, I don't have much time left so it would help if the solution could be as brief as possible.

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help with JAVA Quiz

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Need Help with JAVA Quiz

Posted 25 November 2015 - 03:29 AM

I suspect it's too late.

Wow! That's a lot of code, much of it repeated over and over again. You wouldn't have liked my suggestion, even if it had been made in time for you to implement it. I would have suggested rewriting your code so that ALL questions were posed by ONE method and then evaluated by another single method.

Since you'd like to give the user the option to exit at any time, any method that was waiting for user input would also include the option to enter a response that indicated "exit now." How that response is coded to exit can be done more than one way. Staying consistent with my first suggestion, the exit response would be passed to the answer evaluation method which would then dump the user to an exit.

In future, ANYTIME you're writing nearly the same code more than once, consider how that code could be generalized to a single method that could be used multiple times. In this case, it would not have been hard, and it would have saved you significant effort and made your program much shorter and simpler.

Keep coding!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1