Quizzes

Create a quiz, then make a copy and add in a scoring system

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2821 Views - Last Post: 15 November 2010 - 11:40 AM Rate Topic: -----

#1 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Quizzes

Posted 12 November 2010 - 08:30 AM

Hello again, DiC. My question this time deals with a project concerning a quiz.

My goal is to create a quiz utilizing JOptionPane and decision statements, but I came across an obstacle. The code is as follows for Quiz.java:

// Alphonsus Delgra
// November 3, 2010
// Lab 6-2
// Quiz on Super Mario Bros.
import javax.swing.JOptionPane;
public class Quiz
{
	public static void main(String[] args)
	{
	String correctAnswer = "C";
	String userChoiceString;
	String userChoice;
	userChoice = JOptionPane.showInputDialog(null, 
			"Question 1: What is the name of Mario's brother? \n A. Toad \n B. Wario \n C. Luigi \n D. Koopa");
		if(userChoice == correctAnswer)
				JOptionPane.showMessageDialog(null, "Correct!");
			else
				JOptionPane.showMessageDialog(null, "Wrong answer.");
	}
}



I have to make five questions, so I can just copy and paste in four copies of this one. The problem is that when I compile it, works fine, but when running, even when I put in the correct answer, I still get back the pop-up window saying "Wrong answer." I don't know if I missed something but it worked beforehand yet I forgot the change that I made that returns back "Wrong answer" even if it was the right one. The other question deals with implementing a scoring system. I'm calling it Quiz2.java. Should I use the scoring system after all the questions being answered or start calculating the right and/or wrong answers after each answer? Thanks again for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Quizzes

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Quizzes

Posted 12 November 2010 - 08:55 AM

Don't compare Strings using the == operator. Compare them instead using the equals() method. The == operator compares memory adresses, while the equals() method compares the String values.
Stirngs s1 = "test";
if(s1.equals(s2)){/*code*/}


Was This Post Helpful? 0
  • +
  • -

#3 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Quizzes

Posted 12 November 2010 - 09:07 AM

When I switched the statement, the error read:

Quiz.java:15: incompatible types
found   : java.lang.String
required: boolean
		if(userChoice = correctAnswer)
		              ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



I don't think assignment operators should be used within if statements.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Quizzes

Posted 12 November 2010 - 09:09 AM

Use the equals() method, not the assignment operator and not the == operator. Did you read my last post?
Was This Post Helpful? 0
  • +
  • -

#5 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Quizzes

Posted 12 November 2010 - 09:24 AM

Yes, it's just that the equals method didn't pop up in my chapter which is chapter 6 (equals pops up in Chapter 7 of my book). From past assignments and projects, my professor strictly recommends that I would use material learned from within the current chapter and everything in the past chapters. That's why I was confused at first with the equals() method and had it right before I changed one thing in the code and messed it all up. Is there at least another way besides the equals() method?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Quizzes

Posted 12 November 2010 - 09:30 AM

You could use the toCharArray() method for Strings and compare the individual elements in the arrays. Basically, write your own equals() method. Really, though, the String equals() method is the better way.
Was This Post Helpful? 0
  • +
  • -

#7 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Quizzes

Posted 12 November 2010 - 10:33 AM

After some time reading, I figured out that Arrays is ahead (Chapter 8), but then looked back at Chapter 7's equals() and noticed the difference. After sending an email to my teacher about what content to use and not to use, she replied that as long as you know what the content is, she's fine with that. So all I got to do is just comment in the new change from using the == operators to the equals()method. My code looks like this now:

// Alphonsus Delgra
// November 3, 2010
// Lab 6-2
// Quiz on Super Mario Bros.
import javax.swing.JOptionPane;
public class Quiz
{
	public static void main(String[] args)
	{
	String s1 = "test";
	String s2 = "C";
	String userChoice;
	userChoice = JOptionPane.showInputDialog(null, 
			"Question 1: What is the name of Mario's brother? \n A. Toad \n B. Wario \n C. Luigi \n D. Koopa");
		if(s1.equals(s2))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	}
}



Earlier in this forum topic, macos used "test" as a value for s1. Do I have to change that to correct answer letter (in this case C?)
Was This Post Helpful? 0
  • +
  • -

#8 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Quizzes

Posted 13 November 2010 - 04:59 AM

Bumped for answer
Was This Post Helpful? -1
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Quizzes

Posted 13 November 2010 - 06:02 PM

You would compare userChoice to s2 using the equals() method, not s1 to s2.
Was This Post Helpful? 0
  • +
  • -

#10 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Quizzes

Posted 14 November 2010 - 12:10 AM

Ok, after that I made a short 5-question quiz. The problem I ran into is that after the first question, whether it was right or wrong, the program ended right there. I think it may have been because of the brackets I use for each question, but I don't know if I should include it all in one block. The code I got so far is:

// Alphonsus Delgra
// November 3, 2010
// Lab 6-2
// Quiz on Super Mario Bros.
import javax.swing.JOptionPane;
public class Quiz
{
	public static void main(String[] args)
	{
	String s2 = "C";
	String userChoice;
	userChoice = JOptionPane.showInputDialog(null, 
			"Question 1: What is the name of Mario's brother? \n A. Toad \n B. Wario \n C. Luigi \n D. Koopa");
		if(userChoice.equals(s2))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	}
	{
	String s2 = "B";
	String userChoice;
	userChoice = JOptionPane.showInputDialog(null, 
			"Question 2: What is the item that gives Mario limited invulnerability? \n A. Mushroom \n B. Star \n C. Fire Flower \n D. None of the above");
		if(userChoice.equals(s2))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	}
	{
	String s2 = "A";
	String userChoice;
	userChoice = JOptionPane.showInputDialog(null, 
			"Question 3: What is Peach's last name? \n A. Toadstool \n B. Mario \n C. Koopa \n D. She has no last name");
		if(userChoice.equals(s2))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	}
	{
	String s2 = "D";
	String userChoice;
	userChoice = JOptionPane.showInputDialog(null, 
			"Question 4: Who is the first enemy that Mario faces? \n A. Hammer Bro. \n B. Boo \n C. Bowser \n D. Goomba");
		if(userChoice.equals(s2))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	}
	{
	String s2 = "B";
	String userChoice;
	userChoice = JOptionPane.showInputDialog(null, 
			"Question 5: What game did Waluigi debut in? \n A. Mario Party \n B. Mario Tennis \n C. Lui \n D. Koopa");
		if(userChoice.equals(s2))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Quizzes

Posted 14 November 2010 - 08:47 AM

Make sure you match up your curly braces.
// Alphonsus Delgra
// November 3, 2010
// Lab 6-2
// Quiz on Super Mario Bros.
import javax.swing.JOptionPane;
public class Quiz
{
	public static void main(String[] args)
	{ //start main
	String s2 = "C";
	String userChoice;
	userChoice = JOptionPane.showInputDialog(null, 
			"Question 1: What is the name of Mario's brother? \n A. Toad \n B. Wario \n C. Luigi \n D. Koopa");
		if(userChoice.equals(s2))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
		} //end if
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		} //end else
	} //end main
	{ //rogue brace outside of main
          //none of this code will be executed
	String s2 = "B";


Was This Post Helpful? 1
  • +
  • -

#12 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Quizzes

Posted 14 November 2010 - 12:14 PM

Ok, the quiz works just fine right now. The last two parts of this project asks me to do a scoring system within the quiz. So I should do another if else statement right after each question and do a showMessageDialog at the end of the entire quiz? And the second part is to make another copy of the quiz (call it Quiz2.java), but this time remove the right/wrong answer percentage and have a loop in which the question will repeat itself until you get the right answer. For the first of the two parts, I'll put in whatever the code for the percentage within each question first at the if...else statement part and comment it out to show the change:

// Alphonsus Delgra
// November 3, 2010
// Lab 6-2
// Quiz on Super Mario Bros.
import javax.swing.JOptionPane;
public class Quiz
{
	public static void main(String[] args)
	{
        // int Score = 0;
	String a1 = "C";
	String userChoice1;
	userChoice1 = JOptionPane.showInputDialog(null, 
			"Question 1: What is the name of Mario's brother? \n A. Toad \n B. Wario \n C. Luigi \n D. Koopa");
		if(userChoice1.equals(a1))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
                                // Score = Score + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	String a2 = "B";
	String userChoice2;
	userChoice2 = JOptionPane.showInputDialog(null, 
			"Question 2: What is the item that gives Mario limited invulnerability? \n A. Mushroom \n B. Star \n C. Fire Flower \n D. None of the above");
		if(userChoice2.equals(a2))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
                                // Score = Score + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	String a3 = "A";
	String userChoice3;
	userChoice3 = JOptionPane.showInputDialog(null, 
			"Question 3: What is Peach's last name? \n A. Toadstool \n B. Mario \n C. Koopa \n D. She has no last name");
		if(userChoice2.equals(a3))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
                               // Score = Score + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	String a4 = "D";
	String userChoice4;
	userChoice4 = JOptionPane.showInputDialog(null, 
			"Question 4: Who is the first enemy that Mario faces? \n A. Hammer Bro. \n B. Boo \n C. Bowser \n D. Goomba");
		if(userChoice4.equals(a4))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
                               // Score = Score + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
	String a5 = "B";
	String userChoice5;
	userChoice5 = JOptionPane.showInputDialog(null, 
			"Question 5: What game did Waluigi debut in? \n A. Mario Party \n B. Mario Tennis \n C. Paper Mario \n D. Mario Golf");
		if(userChoice5.equals(a5))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
                                // Score = Score + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
		}
        // JOptionPane.showMessageDialog(null, "Your score is " + Score + " out of 5."
	}
}



For the Quiz2.java, I was thinking instead of the if...else statements, I'll have a do...while statement. That should be the correct step, right?
Was This Post Helpful? 0
  • +
  • -

#13 ninechances  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 188
  • Joined: 08-February 10

Re: Quizzes

Posted 14 November 2010 - 01:51 PM

yes. you are correct. for each question your pseudo code would be something like the following

while (userAnswer doesn't equal the actual answer)
tell the user they were wrong
prompt them to enter another answer
etc...

Just think about your problem in plain English and transfer that into code.

Again, you need to do a while statement for each question.

This post has been edited by ninechances: 14 November 2010 - 01:52 PM

Was This Post Helpful? 0
  • +
  • -

#14 GamingDoc  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 92
  • Joined: 21-February 10

Re: Quizzes

Posted 14 November 2010 - 02:44 PM

Ok transferring my pseudo code, this is what I got for Quiz2 (the one where if the user got the question wrong, the question will reappear again until s/he gets it right).

// Alphonsus Delgra
// November 3, 2010
// Lab 6-2
// Quiz2 on Super Mario Bros.
// Again using equalsIgnoreCase to avoid case sensitivity
import javax.swing.JOptionPane;
public class Quiz2
{
	public static void main(String[] args)
	{
	String a1 = "C";
	String userChoice1;
	userChoice1 = JOptionPane.showInputDialog(null, 
			"Question 1: What is the name of Mario's brother? \n A. Toad \n B. Wario \n C. Luigi \n D. Koopa");
		if(userChoice1.equalsIgnorecase(a1))
		{
			JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
			do
		{
				JOptionPane.showMessageDialog(null, "Wrong answer.");
				userChoice1 = JOptionPane.showInputDialog(null, 
			"Question 1: What is the name of Mario's brother? \n A. Toad \n B. Wario \n C. Luigi \n D. Koopa");
		}
		while(userChoice1 != a1);
	String a2 = "B";
	String userChoice2;
	userChoice2 = JOptionPane.showInputDialog(null, 
			"Question 2: What is the item that gives Mario limited invulnerability? \n A. Mushroom \n B. Star \n C. Fire Flower \n D. None of the above");
		if(userChoice2.equalsIgnorecase(a2))
		{
			JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		do
		{
				JOptionPane.showMessageDialog(null, "Wrong answer.");
				userChoice2 = JOptionPane.showInputDialog(null, 
			"Question 2: What is the item that gives Mario limited invulnerability? \n A. Mushroom \n B. Star \n C. Fire Flower \n D. None of the above");
		}
		while(userChoice2  != a2);
	String a3 = "A";
	String userChoice3;
	userChoice3 = JOptionPane.showInputDialog(null, 
			"Question 3: What is Peach's last name? \n A. Toadstool \n B. Mario \n C. Koopa \n D. She has no last name");
		if(userChoice3.equalsIgnorecase(a3))
		{
			JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		do
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
			userChoice3 = JOptionPane.showInputDialog(null, 
			"Question 3: What is Peach's last name? \n A. Toadstool \n B. Mario \n C. Koopa \n D. She has no last name");
		}
		while(userChoice3 != a3);
	String a4 = "D";
	String userChoice4;
	userChoice4 = JOptionPane.showInputDialog(null, 
			"Question 4: Who is the first enemy that Mario faces? \n A. Hammer Bro. \n B. Boo \n C. Bowser \n D. Goomba");
		if(userChoice4.equalsIgnorecase(a4))
		{
			JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		do
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
			userChoice4 = JOptionPane.showInputDialog(null, 
			"Question 4: Who is the first enemy that Mario faces? \n A. Hammer Bro. \n B. Boo \n C. Bowser \n D. Goomba");
		}
		while(userChoice4 != a4);
	String a5 = "B";
	String userChoice5;
	userChoice5 = JOptionPane.showInputDialog(null, 
			"Question 5: What game did Waluigi debut in? \n A. Mario Party \n B. Mario Tennis \n C. Paper Mario \n D. Mario Golf");
		if(userChoice5.equalsIgnorecase(a5))
		{
			JOptionPane.showMessageDialog(null, "Correct!");
		}
		else
		do
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
			userChoice5 = JOptionPane.showInputDialog(null, 
			"Question 5: What game did Waluigi debut in? \n A. Mario Party \n B. Mario Tennis \n C. Paper Mario \n D. Mario Golf");
		}
		while(userChoice5 != a5);
	}
}



When compiling, I found these errors:

 ----jGRASP exec: javac -g C:\Users\Alphonsus\Documents\Java 2nd\Quiz2.java

Quiz2.java:15: cannot find symbol
symbol  : method equalsIgnorecase(java.lang.String)
location: class java.lang.String
		if(userChoice1.equalsIgnorecase(a1))
		              ^
Quiz2.java:31: cannot find symbol
symbol  : method equalsIgnorecase(java.lang.String)
location: class java.lang.String
		if(userChoice2.equalsIgnorecase(a2))
		              ^
Quiz2.java:47: cannot find symbol
symbol  : method equalsIgnorecase(java.lang.String)
location: class java.lang.String
		if(userChoice3.equalsIgnorecase(a3))
		              ^
Quiz2.java:63: cannot find symbol
symbol  : method equalsIgnorecase(java.lang.String)
location: class java.lang.String
		if(userChoice4.equalsIgnorecase(a4))
		              ^
Quiz2.java:79: cannot find symbol
symbol  : method equalsIgnorecase(java.lang.String)
location: class java.lang.String
		if(userChoice5.equalsIgnorecase(a5))
		              ^
5 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



As for Quiz.java, I wanted to implement a scoring system to it. So I made a few changes to add in and this is what came out:

// Alphonsus Delgra
// November 3, 2010
// Lab 6-2
// Quiz on Super Mario Bros.
// Uses equalsIgnoreCase from Chapter 7 to accept letter answer without being case sensitive
import javax.swing.JOptionPane;
public class Quiz
{
	public static void main(String[] args)
	{
	String a1 = "C";
	String userChoice1;
	int numRight = 0;
	int numWrong = 0;
	double percent = (numRight / 5) * 100;
	userChoice1 = JOptionPane.showInputDialog(null, 
			"Question 1: What is the name of Mario's brother? \n A. Toad \n B. Wario \n C. Luigi \n D. Koopa");
		if(userChoice1.equalsIgnoreCase(a1))
		{
			JOptionPane.showMessageDialog(null, "Correct!");
			numRight = numRight + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
			numWrong = numWrong + 1;
		}
	String a2 = "B";
	String userChoice2;
	userChoice2 = JOptionPane.showInputDialog(null, 
			"Question 2: What is the item that gives Mario limited invulnerability? \n A. Mushroom \n B. Star \n C. Fire Flower \n D. None of the above");
		if(userChoice2.equalsIgnoreCase(a2))
		{
				JOptionPane.showMessageDialog(null, "Correct!");
				numRight = numRight + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
			numWrong = numWrong + 1;
		}
	String a3 = "A";
	String userChoice3;
	userChoice3 = JOptionPane.showInputDialog(null, 
			"Question 3: What is Peach's last name? \n A. Toadstool \n B. Mario \n C. Koopa \n D. She has no last name");
		if(userChoice3.equalsIgnoreCase(a3))
		{
			JOptionPane.showMessageDialog(null, "Correct!");
			numRight = numRight + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
			numWrong = numWrong + 1;
		}
	String a4 = "D";
	String userChoice4;
	userChoice4 = JOptionPane.showInputDialog(null, 
			"Question 4: Who is the first enemy that Mario faces? \n A. Hammer Bro. \n B. Boo \n C. Bowser \n D. Goomba");
		if(userChoice4.equalsIgnoreCase(a4))
		{
			JOptionPane.showMessageDialog(null, "Correct!");
			numRight = numRight + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
			numWrong = numWrong + 1;
		}
	String a5 = "B";
	String userChoice5;
	userChoice5 = JOptionPane.showInputDialog(null, 
			"Question 5: What game did Waluigi debut in? \n A. Mario Party \n B. Mario Tennis \n C. Paper Mario \n D. Mario Golf");
		if(userChoice5.equalsIgnoreCase(a5))
		{
			JOptionPane.showMessageDialog(null, "Correct!");
			numRight = numRight + 1;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Wrong answer.");
			numWrong = numWrong + 1;
		}
	JOptionPane.showMessageDialog(null, "Your have " + numRight + " correct answers.  \n  You have " + numWrong + " wrong answers.  \n Your score is " + percent + "%.  \n Thank you for playing!");
	}
}



When it compiles, it's just fine, but when I run the program, no matter how many questions I answer correctly or incorrectly, I get my percentage to be 0.0%. This tells me that I am placing the equation for the percentage in the wrong spot, but I tried both at the end near the last dialogBox and at the declarations before I start asking me questions.
Was This Post Helpful? 0
  • +
  • -

#15 ninechances  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 188
  • Joined: 08-February 10

Re: Quizzes

Posted 14 November 2010 - 03:43 PM

your conditionals aren't nested inside of your do while loops...that's the first problem.

think about the problem logically. You can't check the answer before you ask it.

This post has been edited by ninechances: 14 November 2010 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2