4 Replies - 1634 Views - Last Post: 07 October 2012 - 08:35 AM Rate Topic: -----

#1 alittleofthegoodlife85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-October 12

Need help using repetition statements in a multiple choice quiz app

Posted 06 October 2012 - 11:11 PM

Hi everyone. First let me say that I am very new to Java and have no experience with any programming languages. So please don't be too harsh.

I am trying to make a multiple choice quiz that includes a "for" and a "do-while" statement as well as a "switch" statement. I understand that the current do-while statements that are in the code are not needed, but they were an attempt to squeeze them in somehow. I have been doing research on these loops and understand how they work, but am not sure how to make them work in this situation.

As of right now the quiz application works perfectly, and does exactly what I want it to do, I just want to include a for loop and make better use of a do-while loop. Any suggestions would be greatly appreciated. Thanks.






import java.util.Scanner;
public class quiz4 
{
	public static void main (String []args)
	{
		System.out.println( "WELCOME TO THE QUIZ\n");
		int c = 0;
	
		Scanner scan = new Scanner ( System.in );
		
		System.out.println( "What Language is this coded in?" );				
		System.out.println( "1 = C++");
		System.out.println( "2 = C#");
		System.out.println( "3 = PC");
		System.out.println( "4 = Java");
		
		String in;
		in = scan.nextLine();
						
		if (in.equals("4"))
		{		
			do 
			{
			System.out.println("Correct\n");
		    c++;
			}
			while (c>1);
			}
		else
		{		    
			System.out.println("wrong\n");
		}
		
		System.out.println( "What is the capital of Montana?" );		
		System.out.println( "1 = Big Sky");
		System.out.println( "2 = Helena");
		System.out.println( "3 = Missoula");
		System.out.println( "4 = Billings");
		
		in=scan.nextLine();
		
		if (in.equals("2"))
		{	
			do
			{
		    System.out.println("Correct\n");
		    c++;
			}
			while (c>2);
			}
		else
		{		    
			System.out.println("wrong\n");
		}
		
		System.out.println( "What color is the sky?" );
		System.out.println( "1 = Orange");
		System.out.println( "2 = White");
		System.out.println( "3 = Blue");
		System.out.println( "4 = Red");
		
		in=scan.nextLine();
		
		if (in.equals("3"))
		{	
			do
			{
		    System.out.println("Correct\n");
		    c++;
			}
		    while (c>3);
			}
		
		else
		{		    
			System.out.println("wrong\n");
		}
		
		System.out.println( "What year is it?" );
		System.out.println( "1 = 1994");
		System.out.println( "2 = 1776");
		System.out.println( "3 = 2012");
		System.out.println( "4 = 3033");
		
		in=scan.nextLine();
		
		if (in.equals("3"))
		{	
			do
			{
		    System.out.println("Correct\n");
		    c++;
			}
			while (c>4);
			}
		else
		{		    
			System.out.println("wrong\n");
		}
		
		System.out.println( "What is the name of our current president?" );
		System.out.println( "1 = Barrack Obama");
		System.out.println( "2 = Bill Clinton");
		System.out.println( "3 = Mitt Romney");
		System.out.println( "4 = Ron Paul");
		
		in=scan.nextLine();
		
		if (in.equalsIgnoreCase("1"))
		{
			do
			{
		    System.out.println("Correct\n");
		    c++;
			}
			while (c>5);
			}
		else
		{		    
			System.out.println("wrong\n");
		}

		
		System.out.println("You answered " + c + " out of 5 correctly");
		System.out.println("Your Score is " + 100 * c/5 + "%");
		
		
		
		int answer;
		answer = c;
		
		switch (answer)
		
		{
		case 0:
			System.out.println ("Were you even trying?");
			break;
		case 1:
			System.out.println ("Time to brush up on your knowledge");
			break;
		case 2:
			System.out.println ("Time to brush up on your knowledge");
			break;
		case 3:
			System.out.println ("Time to brush up on your knowledge");
			break;
		case 4:
			System.out.println ("Very Good");
			break;
		case 5:
			System.out.println ("Excellent");
			break;
		
		}	
		}
	
}




Is This A Good Question/Topic? 0
  • +

Replies To: Need help using repetition statements in a multiple choice quiz app

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Need help using repetition statements in a multiple choice quiz app

Posted 07 October 2012 - 03:57 AM

They way you use do-while here is comletely redundant, so I would remove that if I were you. What you could do is have a do-while-loop around almost your entire code, so that when the user has done the quiz once he/she is asked again if he/she wants another go, then let the while loop continue as long as the answer is "y" or "yes".
If you absolutely want to use a for-loop aswell you could put all of the questions and alternatives in an array (and the corresponding answers in another) which you loop through, that way the if/else statement only need to appear once.
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Need help using repetition statements in a multiple choice quiz app

Posted 07 October 2012 - 04:00 AM

Your question isn't clear. Are you simply asking for advice on how to add a for loop to the code where it may not be needed, similarly to as you've done with the do/while loops?

In general, a for loop would be used when the number of times the loop needs to execute is known or can be calculated before the loop starts. In the case of quiz, a likely use for a for loop would be to loop as many times as their are questions, presenting each question and gathering the answer. That requires a slightly different design than you currently have, but it might be an improvement.

Another use for a for loop in a quit might be to tally the correct answers to calculate the score after the test is complete. Your use of c to keep track of correct answers eliminates the need for a grading loop. (Why didn't you just switch on c?)

Yes, your current use of the do/while loop is rubbish, but you knew that. A more appropriate use of a do/while loop in a quiz would be as a validator of the user's answer. The loop would ensure the user gave a valid response without regard for whether it was correct or not.
Was This Post Helpful? 0
  • +
  • -

#4 alittleofthegoodlife85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-October 12

Re: Need help using repetition statements in a multiple choice quiz app

Posted 07 October 2012 - 07:29 AM

Basically this is my first Java assignment all of the criteria has been met except for the following:

"(Note: Since the chapter deals with repetition (for, do-while) and selection (switch) statements, maximum credit will be given for use of these statements.)"


I am not sure if I need to include all of these in my quiz application or just one or two examples. If possible I would prefer a combination of all three. I keep rereading my textbook and re-watching tutorials but I still don't get how to make it work.


To GregBrannon as far as not using "c" in the switch goes, I have no idea why I didn't use it. I changed it as soon as I read your comment though. Thanks for the feedback.


I am still playing around with the the suggestions that were made, however I am very much a noob so it takes me a good amount of time to figure out how to go about each suggestion. I only have about a weeks worth of learning Java under my belt, but it's slowly starting to click.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1399
  • View blog
  • Posts: 3,101
  • Joined: 05-April 11

Re: Need help using repetition statements in a multiple choice quiz app

Posted 07 October 2012 - 08:35 AM

Did you learn about arrays yet?
If you did, then you could look into using them as well

Imagine having a 2D array which is structured like:
[answer], [choice], [choice], [choice], [choice]
[answer], [choice], [choice], [choice], [choice]

Each row in the array will have the answer at index 0, and the questions at 1-n where n is the length of the array - 1

An array of this kind will give good possibilities to use loops :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1