?
Solved

Java Programming . I need help.

Posted on 2003-12-07
4
Medium Priority
?
557 Views
Last Modified: 2010-08-05
Hi, I am a Java student. I am writing a quiz program that must contain 5 questions and answers that must be either y for yes, n for no or true or false.  I have written the program to contain 2 questions so far. When i try to put in another question my program will not compile. Also for my two questions that work, when i put in the correct answer it is giving me the incorrect reply.  I double checked my program and i cannot find the problem. Can someone please help. Thanks. Below is what i have so far....

public class Quiz
{
public static void main (String args[]) throws Exception
{
String astronomyQuestion = new String("Is the sun a star? (enter y or n):");
String hobbyQuestion = new String("Is Hocky played with a ball? (enter y or n):");
String question;
char selection, answer;
int numCorrect = 0;

System.out.println("Select Quiz");
System.out.print("Enter a (Hobby), b (Astronomy), c (Music) d (Movies) e (Java) f (quit): ");
selection = (char)System.in.read();
System.in.read();
System.in.read();

switch(selection)
{
case 'a':
question = hobbyQuestion;
answer = 'n';
break;
case 'b':
question = astronomyQuestion;
answer = 'y';
break;
default:
return;
}

System.out.print(question);
char response = (char)System.in.read();
System.in.read();
System.in.read();

if (response == answer) {
System.out.println("Correct!");
numCorrect++;
}
else
System.out.println("Incorrect!" +
"\nCorrect answer is " + answer);

System.out.println("Number of correct answers: " + numCorrect);

}
}
0
Comment
Question by:Goldbyrd
4 Comments
 
LVL 9

Accepted Solution

by:
bhagyesht earned 150 total points
ID: 9894729
well i tried the following code I added another question and it works fine.
public class Quiz
{
      public static void main (String args[]) throws Exception
      {
            String astronomyQuestion = new String("Is the sun a star? (enter y or n):");
            String hobbyQuestion = new String("Is Hocky played with a ball? (enter y or n):");
            String MusicQuestion = new String("Is guitar an instrument? (enter y or n):");
            String question;
            char selection, answer;
            int numCorrect = 0;

            System.out.println("Select Quiz");
            System.out.print("Enter a (Hobby), b (Astronomy), c (Music) d (Movies) e (Java) f (quit): ");
            selection = (char)System.in.read();
            System.in.read();
            System.in.read();

            switch(selection)
            {
            case 'a':
                  question = hobbyQuestion;
                  answer = 'n';
                  break;
            case 'b':
                  question = astronomyQuestion;
                  answer = 'y';
                  break;
            case 'c':
                  question = MusicQuestion;
                  answer = 'y';
                  break;
            default:
                  return;
            }

            System.out.print(question);
            char response = (char)System.in.read();
            System.in.read();
            System.in.read();

            if (response == answer) {
                  System.out.println("Correct!");
                  numCorrect++;
            }
            else
                  System.out.println("Incorrect!" +
                                             "\nCorrect answer is " + answer);

            System.out.println("Number of correct answers: " + numCorrect);

      }
}

can you give more details on the error?
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 9895075
hm...i don't understan what's wrong ? :)
0
 

Author Comment

by:Goldbyrd
ID: 9897714
I still could not get the program to work. It still has a flaw, When i type in a,bor c for the question options it does nothing. Any ideas. Here is the complete program i made. See if you can do anything with it. Thanks.

public class Quiz
{
     public static void main (String args[]) throws Exception
     {
          String astronomyQuestion = new String("Is the sun a star? (enter y or n):");
          String hobbyQuestion = new String("Is Hocky played with a ball? (enter y or n):");
          String MusicQuestion = new String("Is guitar an instrument? (enter y or n):");
          String MovieQuestion = new String("Was Kevin Costner in Dances with wolves? (enter y or n):");
          String javaQuestion = new String("Is Java better than C++ (enter y or n):");
          String question;
          char selection, answer;
          int numCorrect = 0;

          System.out.println("Select Quiz");
          System.out.print("Enter a (Hobby), b (Astronomy), c (Music) d (Movies) e (Java) f (quit): ");
          selection = (char)System.in.read();
          System.in.read();
          System.in.read();

          switch(selection)
          {
          case 'a':
               question = hobbyQuestion;
               answer = 'n';
               break;
          case 'b':
               question = astronomyQuestion;
               answer = 'y';
               break;
          case 'c':
               question = MusicQuestion;
               answer = 'y';
               break;
          case 'd':
               question = MovieQuestion;
               answer = 'y';
               break;
          case 'e':
               question = javaQuestion;
               answer = 'y';
               break;

          default:
               return;
          }

          System.out.print(question);
          char response = (char)System.in.read();
          System.in.read();
          System.in.read();

          if (response == answer) {
               System.out.println("Correct!");
               numCorrect++;
          }
          else
               System.out.println("Incorrect!" +
                                      "\nCorrect answer is " + answer);

          System.out.println("Number of correct answers: " + numCorrect);

     }
}
0
 
LVL 1

Expert Comment

by:nil1st
ID: 9899166
Well I ran that Program and it worked fine in my computer also, may be you forgot to press Enter after pressing 'a', 'b' or 'c'.
Also if you doing all that, Please tell us what Procedure are you taking to compile the programe, or maybe what Java Editor are you using to run the programe.


Nirav
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Six Sigma Control Plans
Starting up a Project

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question