Java Programming . I need help.

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);

}
}
GoldbyrdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bhagyeshtCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dualsoulCommented:
hm...i don't understan what's wrong ? :)
0
GoldbyrdAuthor Commented:
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
nil1stCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.