Solved

Java Programming . I need help.

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now