954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with Quiz code

Can someone help me to get this code to run. What am I missing. Any help is greatly appreciated.

import java.io.*;
public class Quiz
{
public static void main(String args[])
{
int correctCounter;
InputStreamReader reader;
BufferedReader buffer; keyboard = new
BufferedReader (new InputStreamReader (System.in));
}
String seekResponse;
System.out.println("Welcome to Hoffman School .. ");
{
// choose the subject
System.out.println("enter h for humanities, e for English or x for exit");
seekResponse = keyboard.readline();
if (seekResponse.equals ("h"); //do humanities quiz
{ System.out.println("The Declaration of Independence is the same as the
constitution");
seekResponse = keyboard.readline();
if (seekResponse=("t"));
System.out.println("incorrect answer");
if (seekResponse.equals ("f"));
System.out.println("correct");
System.out.println("At the Boston Tea Party the people threw coffee overboard");
if (seekResponse.equals ("t"));
System.out.println("incorrect answer");
If (seekResponse.equals ("f"));
System.out.println("correct");
}
else if (seekResponse.equals ("e"); //do English quiz

{System.out.println("A period goes at the end of a question");
seekResponse = keyboard.readline();
if (seekResponse.equals ("t"));
System.out.println("incorrect answerz");
if (seekResponse.equals ("f"));
System.out.println("correct");
System.out.println("Most sentences begin with a capital letter");
if (seekResponse.equals ("t"));
System.out.println("incorrect answer");
If (seekResponse.equals ("f"));
System.out.println("correct");
}
else if (seekResponse.equals ("x"));
exit// do exit logic
}
}

Vcrossley
Newbie Poster
2 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

Here's the modified code

import java.io.*;
public class Quiz
{
	public static void main(String args[]) 
	{
		int correctCounter;
		InputStreamReader reader;
		BufferedReader buffer; 
		keyboard = new BufferedReader (new InputStreamReader (System.in));
	}

	String seekResponse;
	System.out.println("Welcome to Hoffman School .. ")
	{
		// choose the subject
		System.out.println("enter h for humanities, e for English or x for exit");
		seekResponse = keyboard.readline();
		if (seekResponse.equals ("h") //do humanities quiz
		{ 
			System.out.println("The Declaration of Independence is the same as the constitution");
			seekResponse = keyboard.readline();
			if (seekResponse=("t"))
				System.out.println("incorrect answer");
			if (seekResponse.equals ("f"))
				System.out.println("correct");
				System.out.println("At the Boston Tea Party the people threw coffee overboard");
			if (seekResponse.equals ("t"))
				System.out.println("incorrect answer");
			If (seekResponse.equals ("f"))
				System.out.println("correct");
		}
		else if (seekResponse.equals ("e") //do English quiz
		{
			System.out.println("A period goes at the end of a question");
			seekResponse = keyboard.readline();
			if (seekResponse.equals ("t"))
				System.out.println("incorrect answerz");
			if (seekResponse.equals ("f"))
			{
				System.out.println("correct");
				System.out.println("Most sentences begin with a capital letter");
			}
			if (seekResponse.equals ("t"))
				System.out.println("incorrect answer");
			If (seekResponse.equals ("f"))
				System.out.println("correct");
		}
		else if (seekResponse.equals ("x")) 
			exit// do exit logic
		}
	}
}


You shouldn't put a ; at the end of EVERY line. like:

if(1 > 2); // <-- that ; shouldn't be there

The other problem is that if you have more than one statment after an if, you need a curly bracket like this:

if(10 < 200)
    System.out.println("this is ok to do");
// That was ok

if(10 < 200){
    System.out.println("First Line");
    System.out.println("Second Line");
}

If you leave out the curly braces on that second example you will always print the second line, which wouldn't be the desired result.

Lastly, you had a capital I in an if statment. That won't work. Java is case sensitive.
Hope that helped.

Iron_Cross
Junior Poster
117 posts since Jul 2003
Reputation Points: 46
Solved Threads: 2
 

Thanks so much for your help. All I have to do now is add the counter to this.

Vcrossley
Newbie Poster
2 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You