DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2005
    Location
    Airzona
    Posts
    7

    Help Application

    Ok we have to make a 10 question quiz for my computer programming class this is what i have so far and im gettting so pissed off cuz it wont see if the answer they put in is right or not

    Main file-
    public class Quiz
    {
    private String question;
    private String choiceA;
    private String choiceB;
    private String choiceC;
    private String choiceD;
    private String correctAnswer;
    private String rightAnswer;

    Quiz(String q, String CA, String CB, String CC, String CD,
    String cAnswer)
    {
    question = q;
    choiceA = CA;
    choiceB = CB;
    choiceC = CC;
    choiceD = CD;
    correctAnswer = cAnswer;

    }
    public String getQuestion()
    {
    return question;
    }
    public String getChoiceA()
    {
    return choiceA;
    }
    public String getChoiceB()
    {
    return choiceB;
    }
    public String getChoiceC()
    {
    return choiceC;
    }
    public String getChoiceD()
    {
    return choiceD;
    }
    public String getCorrectAnswer()
    {
    return correctAnswer;
    }
    public String getRightAnswer()
    {
    if(answer == correctAnswer)
    rightAnswer = "Correct!";
    if(answer == correctAnswer)
    rightAnswer = " Incorrect";
    }
    }

    Then a second file

    public class ShowQuiz
    {
    static final private String HEADING = "Quiz";
    public static void main(String[] args) throws Exception
    {
    String answer = "";
    char userInput = ' ';
    char correctAnswer = ' ';

    Quiz q1 = new Quiz("What is your name?",
    "\nA. Joe", "\n. Bob" ,
    "C. Jack \n","D. Derek\n", 'A');


    Quiz [] allQ = {q1};

    System.out.print("\t\n" + HEADING);
    System.out.print("\n=============");
    for (int i = 0; i < 1; ++i)
    {
    System.out.print("\n" + allQ[i].getQuestion());
    System.out.print("\n" + allQ[i].getChoiceA());
    System.out.print("\n" + allQ[i].getChoiceB());
    System.out.print("\n" + allQ[i].getChoiceC());
    System.out.print("\n" + allQ[i].getChoiceD());
    }
    System.out.print("Enter your");
    answer = getStr("answer");
    answer = getStr.getChar;

    }
    public static String getStr (String title) throws Exception
    // Returns and input string.
    {
    char ch;

    String str= "";

    System.out.print("\t" + title + ": ");
    do
    {
    ch = (char)System.in.read();
    str += ch;
    }
    while ((int)ch !=13); // 13 is the first byte value of Enter key.
    System.in.read(); // Second byte occupied by the Enter key.

    return str;
    }
    }

    thanks for any help

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I haven't gone through all the code, but this one is strange...
    Code:
    public String getRightAnswer() {
      if(answer == correctAnswer)
        rightAnswer = "Correct!";
      if(answer == correctAnswer)
        rightAnswer = " Incorrect";
    }
    And one more thing;
    string comparison is done using the equals method, if you use the '==' operator
    you are comparing pointer values, not string values.
    eschew obfuscation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links