Solved

Java Programming . I need help.

Posted on 2003-12-07
4
549 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now