| | |
Java Help Needed.......
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
This code is meant to be a simple quiz, but I am getting 4 errors that I cannot figure out. Thanks in advance.
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 Dan's Quiz .. ");
{
// choose the subject
System.out.println("enter M for Math, S for Science or x for exit");
seekResponse = keyboard.readline();
if (seekResponse.equals ("M")) //Select the Math quiz
{
System.out.println("What is the sum of 120 & 120?"); //Math Question1
System.out.printIn("a. 0");
System.out.printIn("b. 240");
System.out.printIn("c. 220");
System.out.printIn("Press X to exit");
seekResponse = keyboard.readline();
if (seekResponse=("a"))
System.out.println("incorrect answer");
if (seekResponse.equals ("b"))
System.out.println("correct");
if (seekResponse.equals ("c"))
System.out.println("incorrect answer");
else if (seekResponse.equals ("x"))
exit; // do exit logic
System.out.println("What is the product of 12 & 12?"); //Math Question2 Line34
System.out.printIn("a. 144");
System.out.printIn("b. 24");
System.out.printIn("c. 1");
System.out.printIn("Press X to exit");
if (seekResponse.equals ("a"))
System.out.println("correct answer");
if (seekResponse.equals ("b"))
System.out.println("incorrect answer");
if (seekResponse.equals ("c"))
System.out.println("incorrect answer");
else if (seekResponse.equals ("x"))
exit;// do exit logic
}
else if (seekResponse.equals ("S")); //Select the Science quiz
{
System.out.println("A snake is a member of which animal group?"); //Science Question1
seekResponse = keyboard.readline();
System.out.printIn("a. Mammal");
System.out.printIn("b. Amphibian");
System.out.printIn("c. Reptile");
if (seekResponse.equals ("a"))
System.out.println("incorrect answer");
if (seekResponse.equals ("b"))
System.out.println("incorrect answer");
if (seekResponse.equals ("c"))
System.out.println("correct");
}
{
else if (seekResponse.equals ("x"))
exit;// do exit logic
}
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 Dan's Quiz .. ");
{
// choose the subject
System.out.println("enter M for Math, S for Science or x for exit");
seekResponse = keyboard.readline();
if (seekResponse.equals ("M")) //Select the Math quiz
{
System.out.println("What is the sum of 120 & 120?"); //Math Question1
System.out.printIn("a. 0");
System.out.printIn("b. 240");
System.out.printIn("c. 220");
System.out.printIn("Press X to exit");
seekResponse = keyboard.readline();
if (seekResponse=("a"))
System.out.println("incorrect answer");
if (seekResponse.equals ("b"))
System.out.println("correct");
if (seekResponse.equals ("c"))
System.out.println("incorrect answer");
else if (seekResponse.equals ("x"))
exit; // do exit logic
System.out.println("What is the product of 12 & 12?"); //Math Question2 Line34
System.out.printIn("a. 144");
System.out.printIn("b. 24");
System.out.printIn("c. 1");
System.out.printIn("Press X to exit");
if (seekResponse.equals ("a"))
System.out.println("correct answer");
if (seekResponse.equals ("b"))
System.out.println("incorrect answer");
if (seekResponse.equals ("c"))
System.out.println("incorrect answer");
else if (seekResponse.equals ("x"))
exit;// do exit logic
}
else if (seekResponse.equals ("S")); //Select the Science quiz
{
System.out.println("A snake is a member of which animal group?"); //Science Question1
seekResponse = keyboard.readline();
System.out.printIn("a. Mammal");
System.out.printIn("b. Amphibian");
System.out.printIn("c. Reptile");
if (seekResponse.equals ("a"))
System.out.println("incorrect answer");
if (seekResponse.equals ("b"))
System.out.println("incorrect answer");
if (seekResponse.equals ("c"))
System.out.println("correct");
}
{
else if (seekResponse.equals ("x"))
exit;// do exit logic
}
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
You should try and put your code into code blocks next time but here's what I see wrong:
Here's what I did for this as an example for you and it is commented out so that you can see what I did in it.
Also I would like to ask what type of environment your using to write java in.
- keyboard is not declared
- seekResponse.equals ("M") checks for an upper case letter same with seekResponse.equals ("s")
- if (seekResponse=("a")) should be if(seekResponse.equals("a"))
- exit; should be System.exit(1);
Here's what I did for this as an example for you and it is commented out so that you can see what I did in it.
Java Syntax (Toggle Plain Text)
import java.io.*; public class Test { public static void main(String[] args) throws IOException { //Create instances of the InputStream and BufferedReader InputStreamReader reader = new InputStreamReader(System.in); //Buffer the inputstream BufferedReader buffer = new BufferedReader(reader); //Create the variables counter, seeker //counter is the number of correct answers incresed by one everytime a correct answer is supplied //seeker is used to store keyboard input to check for the type of quiz and answers int correctCounter = 0; String seeker; //Say hello to the user and display an options menu System.out.println("Welcome to [Name] Quiz..."); System.out.println("Enter 'M' for math, 'S' for scients, or 'X' to exit..."); //Reference the buffer.readLine() command to seeker to grab the input seeker = buffer.readLine(); //select the math quiz //Must use the equals method to compare strings == is invalid //seeker.equalsIgnoreCase(String) takes input regardless of lowercase and uppercase so m is equal to M if(seeker.equalsIgnoreCase("m")) { System.out.println("What is the sum of 5 + 5?"); System.out.print("A. 10\n B. 0\n C. 225"); //reference the buffer.readLine() command to seeker to grab the input seeker = buffer.readLine(); //if 'a' is typed display correct answer if(seeker.equalsIgnoreCase("a")) { System.out.println("Correct Answer!"); //Increase the number of correct answers the user has answered correctCounter += 1; } //if 'b' is typed display incorrect answer else if(seeker.equalsIgnoreCase("b")) { System.out.println("Incorrect Answer..."); } //if 'c' is typed display incorrect answer else if(seeker.equalsIgnoreCase("c")) { System.out.println("Incorrect Answer..."); } else //the user typed invalid input { System.out.println("Invalid input, try again."); //check for input again seeker = buffer.readLine(); } } //exit the program //if the input is 'x' exit the program else if(seeker.equalsIgnoreCase("x")) { //ends the program nicely System.exit(1); } //Tell the user to try again if they type anything else else { System.out.println("Invalid Input! Please try again!"); seeker = buffer.readLine(); } } }
Also I would like to ask what type of environment your using to write java in.
![]() |
Similar Threads
- What's the HARDEST program you've written? (Computer Science)
- hi im new to java project (Java)
- Java Help needed (Community Introductions)
- JAVA HELP NEEDED, for plotting, URGENT (Java)
- Beginning Programming with Java (Java)
- Mortgage Program help (Java)
Other Threads in the Java Forum
- Previous Thread: identifying a carriage return in string
- Next Thread: Help with Exercise 7.2
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint 2dgraphics android api apple applet application applications arguments array arrays automation bank bidirectional binary bluetooth chat class classes client code collision component database db development draw eclipse eclipsedevelopment error event exception file fractal game givemetehcodez graphics gui helpwithhomework homework html ide image input integer integration j2me jarfile java javadesktopapplications javafx javaprojects jmf jni jpanel julia learningresources linux list loop map method methods mobile mysql netbeans newbie number oracle print problem producer program programming project projectideas recursion researchinmotion scanner screen server service set size sms socket sort sorting sql sqlserver state string swing test text-file threads time tree web windows






