Click here to Skip to main content
15,895,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
we are coding a quiz on netbeans basically what the submit button does is when you click it, it's supposed to look for a match of the answer you selected from the correctAns string and adds a point if it's correct, if not it displays "wrong answer" on the jlabel and moves on with the next question but,,, it doesn't..what's wrong with the code?
tysm! apologies as i only have very limited knowledge of java.

the addedScore is defined from
Java

int randNumbersSet[] = new int[15];
    int questionNo = 0, legitQuestionNo = 0, newScore = 0, oldScore = 0, addedScore = 0;
    String answer = "";


oldScore is from a method called scoreCounter
Java
public int scoreCounter(int oldScore){
    return(oldScore + addedScore);
}


so if you score +1, addedScore will be added to oldScore



Java
private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {                                       
    if(answer.equals(correctAnsSet[randNumbersSet[questionNo]])){
        addedScore = 1;
    
    }
    else{
        addedScore = 0;
        quizQuezo.setText("Sorry, wrong answer.");
    }
    
    oldScore = Integer.parseInt(Score.getText());
    newScore = scoreCounter(oldScore);
    
    if(questionNo == randNumbersSet.length - 1){
        quizQuezo.setText("Game Over!");
        
        this.setVisible(false);
        QuizEnd h1 = new QuizEnd(newScore);
        h1.setVisible(true);
    }
    else{
        questionNo++;
        testSetter();
    }
}


What I have tried:

well i haven't tried anything but identify the problem. as i've said before i'm not knowledgeable enough to solve this but i need to as it's for a school requirement.
Posted
Updated 25-Jun-20 5:19am
v3
Comments
Richard MacCutchan 25-Jun-20 10:02am    
What is the following statement supposed to do?
if(answer.equals(correctAnsSet[randNumbersSet[questionNo]])){
Member 14873171 25-Jun-20 10:25am    
if the answer matches any of the correct answers from the string correctAnsSet, it will add a +1 score. randNumberSet is a question randomizer.
Richard MacCutchan 25-Jun-20 10:32am    
OK, so what actually happens when you click the button? Remember we cannot see the code that checks the answer or the various strings that make up the questions and answers.

[edit]
The actual test will only compare with a single element. So if the value returned from correctAnsSet[randNumbersSet[questionNo]] is not the correct one it will not bother testing any more. You need to go through all the answers to find a match.
Member 14873171 25-Jun-20 10:43am    
when you click the button it's supposed to add +1 to addedScore when you pick the correct answer if not, no score is added and it will advance to the next question. from what i understand it's the submit button itself that checks the answers from the strings and determines if it's right or not through if/else since i don't see any separate code to check if the answers are right or wrong ;-;
Richard MacCutchan 25-Jun-20 10:50am    
" from what i understand"
Well if you do not understand your own code I don't know how you expect anyone else to. As i already mentioned, we cannot see what is in answer, correctAnsSet, randNumbersSet or questionNo, so we have no way of telling whether it is correct or not. Also, your code does not add 1 to addedScore, it just sets it to 1 or 0.

1 solution

Quote:
what's wrong with the code?

Your code sets addedScore accordingly to answer is correct or not, but you do nothing with it.
You get oldscore from an objet (GUI ?) but it is not updated after that.
 
Share this answer
 
Comments
Member 14873171 25-Jun-20 10:36am    
well addedScore is defined from

int questionNo = 0, legitQuestionNo = 0, newScore = 0, oldScore = 0, addedScore = 0;

so if the answer you chose is present or matches the list of correct answers from the string correctAnsSet, +1 is added to addedScore but it doesn't do that. hope i explained that properly ;-;
Patrice T 25-Jun-20 10:55am    
We can't guess what you didn't told us.
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900