Solved

A scoring system

Posted on 2006-11-20
17
271 Views
Last Modified: 2010-03-31
I already created 2 strings 'questions' n 'answers' for a simple quiz program.

Now to do a scoring system. Still dont know how to do it. I try matching the System.out.println(" is correct!");
meaning if it displays " is correct" then counter j++

What is the easiest and simple way to solve this problem?
another way i can think of is when user_input = answers then counter count up.
but i got about 10 questions, dont know how to do it using string.
0
Comment
Question by:ishcmc
[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
  • 9
  • 8
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17981707
Surely if you know which are correct, you can increment a variable. Have you got no control of the program  printing 'is  correct'?
0
 

Author Comment

by:ishcmc
ID: 17981746
what do u mean?
0
 

Author Comment

by:ishcmc
ID: 17981754
here is a method from my program to display questions n answers

Scanner sc = new Scanner(System.in);
       
        for (int i=0; i<=6; i++)
        {    
           
            System.out.print(questions[i]);
            String user_input = sc.next();
       
            if (user_input.compareToIgnoreCase(answers[i])==0)
            {
                System.out.print(user_input);
                System.out.println(" is correct");                
            }
       
            else
            {
                System.out.print(user_input);
                System.out.println(" is incorrect");
                System.out.println("correct answer is: " +(answers[i]));                
            }
           
/*
            int score = j;
           
            System.out.println();
            System.out.print("You scored ");
            System.out.print(score);
            System.out.print(" out of 6");
*/
       
        }


the codes inside the comment is what needed to be solved.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17981849
Scanner sc = new Scanner(System.in);
int numCorrect, numIncorrect;
numCorrect = numIncorrect = 0;
       
        for (int i=0; i<=6; i++)
        {    
           
            System.out.print(questions[i]);
            String user_input = sc.next();
       
            if (user_input.compareToIgnoreCase(answers[i])==0)
            {
                numCorrect++;
                System.out.print(user_input);
                System.out.println(" is correct");                
            }

(I'll leave it to you to fill in the rest)
0
 

Author Comment

by:ishcmc
ID: 17982079
wow that was easy just the numCorrect++
the thing is now after each answer it immidiately displays the score
I dont want this, I want it to display after all the questions.
0
 

Author Comment

by:ishcmc
ID: 17982120
Oh i know why, cos i put it in the for loop.
Compiled and ran again but got this error after running all the questions and doesnt display the score

java.lang.ArrayIndexOutOfBoundsException: 6
      at SimpleQuiz.scoreQuestions(SimpleQuiz.java:56)
      at SimpleQuiz.main(SimpleQuiz.java:38)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17982141
Get rid of all the System.out stuff and put this at the end:

System.out.printf("You scored %d correct answers and %d incorrect answers out of %d\n", numCorrect, numIncorrect, 6);
0
 

Author Comment

by:ishcmc
ID: 17982164
i think it's because i put it in the sub method, i should put this in the main
but how do i do this numCorrect++ if all the questions are in the sub?
0
 

Author Comment

by:ishcmc
ID: 17982180
this

System.out.printf("You scored %d correct answers and %d incorrect answers out of %d\n", numCorrect, numIncorrect, 6);

is too advanced for me right now. could u explain what's going on?
ie the f after print, %d etc
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17982193
You need to put those variables in the method in which you're looping and asking questions
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17982219
>>... too advanced for me right now. could u explain what's going on?

It's just formatting the output. You can replace that with

System.out.println("You scored " + numCorrect + " correct answers and " + numIncorrect + " incorrect answers out of 6");
0
 

Author Comment

by:ishcmc
ID: 17982261
>>You need to put those variables in the method in which you're looping and asking questions

what do u mean?
0
 

Author Comment

by:ishcmc
ID: 17982269
putting the score in the sub method, no need to put it in the main?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17982280
>>what do u mean?

numCorrect and numIncorrect should be declared in the same method as the one that asks the questions
0
 

Author Comment

by:ishcmc
ID: 17982333
ok, when i ran the program, it displays all the questions correctly but when it hits the

System.out.println("You scored " + numCorrect + " correct answers and " + numIncorrect + " incorrect answers out of 6");

it gives the following:

java.lang.ArrayIndexOutOfBoundsException: 6
      at SimpleQuiz.scoreQuestions(SimpleQuiz.java:56)
      at SimpleQuiz.main(SimpleQuiz.java:38)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17982419
>>for (int i=0; i<=6; i++)

For that, you need to have 7 questions
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17982449
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use lov values 2 72
Java program running SQL query 5 48
Need help aligning JRadioButton that put in a box using GridBagLayout 2 27
jmeter usage 4 35
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

756 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