?
Solved

Java Programming . I need help.

Posted on 2003-12-07
4
Medium Priority
?
555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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.
In this post we will learn different types of Android Layout and some basics of an Android App.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in 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 …
Suggested Courses

800 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