943,625 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 12588
  • Java RSS
Aug 3rd, 2004
0

Need help with Quiz code

Expand Post »
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
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vcrossley is offline Offline
2 posts
since Jul 2004
Aug 4th, 2004
0

Re: Need help with Quiz code

Here's the modified code
Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. public class Quiz
  3. {
  4. public static void main(String args[])
  5. {
  6. int correctCounter;
  7. InputStreamReader reader;
  8. BufferedReader buffer;
  9. keyboard = new BufferedReader (new InputStreamReader (System.in));
  10. }
  11.  
  12. String seekResponse;
  13. System.out.println("Welcome to Hoffman School .. ")
  14. {
  15. // choose the subject
  16. System.out.println("enter h for humanities, e for English or x for exit");
  17. seekResponse = keyboard.readline();
  18. if (seekResponse.equals ("h") //do humanities quiz
  19. {
  20. System.out.println("The Declaration of Independence is the same as the constitution");
  21. seekResponse = keyboard.readline();
  22. if (seekResponse=("t"))
  23. System.out.println("incorrect answer");
  24. if (seekResponse.equals ("f"))
  25. System.out.println("correct");
  26. System.out.println("At the Boston Tea Party the people threw coffee overboard");
  27. if (seekResponse.equals ("t"))
  28. System.out.println("incorrect answer");
  29. If (seekResponse.equals ("f"))
  30. System.out.println("correct");
  31. }
  32. else if (seekResponse.equals ("e") //do English quiz
  33. {
  34. System.out.println("A period goes at the end of a question");
  35. seekResponse = keyboard.readline();
  36. if (seekResponse.equals ("t"))
  37. System.out.println("incorrect answerz");
  38. if (seekResponse.equals ("f"))
  39. {
  40. System.out.println("correct");
  41. System.out.println("Most sentences begin with a capital letter");
  42. }
  43. if (seekResponse.equals ("t"))
  44. System.out.println("incorrect answer");
  45. If (seekResponse.equals ("f"))
  46. System.out.println("correct");
  47. }
  48. else if (seekResponse.equals ("x"))
  49. exit// do exit logic
  50. }
  51. }
  52. }

You shouldn't put a ; at the end of EVERY line. like:
Java Syntax (Toggle Plain Text)
  1. 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:
Java Syntax (Toggle Plain Text)
  1. if(10 < 200)
  2. System.out.println("this is ok to do");
  3. // That was ok
  4.  
  5. if(10 < 200){
  6. System.out.println("First Line");
  7. System.out.println("Second Line");
  8. }
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.
Reputation Points: 46
Solved Threads: 2
Junior Poster
Iron_Cross is offline Offline
117 posts
since Jul 2003
Aug 4th, 2004
0

Re: Need help with Quiz code

Thanks so much for your help. All I have to do now is add the counter to this.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vcrossley is offline Offline
2 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Error message when running simple Java pgm
Next Thread in Java Forum Timeline: Need help with this email form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC