1

Homework assignment Suggestions only please as I wish very much to learn this as second nature! The goal is to create an array with a user specified question amount (array size) followed by an answer key (said array size now filled). Then to have the user input the "students" answers to check against key. I wrote all that no worries. Works lovely. The issue I am having is in two areas:

  • Create a loop that asks to grade another quiz.
  • Have it only check/score/calculate every other answer. ie: even answers only.

I have used a do/while loop to continue checking but couldn't get a sentinel value to stick. Also depending on where I placed it, the answers kept coming up as the first check. So I am unsure as to where to place and how to write it. I even tried to use a for loop boxing in the array and student answer portion to no avail. As regards to having it check every other one, I thought of modifying the count of "i" to something like ((i+1)*2) instead of i++ for the two for loops but I just get errors as that seems to not be proper at all.

Thank you in advance!

import java.util.Scanner;
import java.text.NumberFormat;

public class EvenQuizzes {
    public static void main(String[] args) {
        int quizQuest = 0, count = 0;
        double percentTotal = 0.0;
        Scanner scan = new Scanner(System.in);
        System.out.print("How many questions are in the quiz? Or enter 0 to quit. ");
        quizQuest = scan.nextInt();
        int[] answers = new int[quizQuest]; // scan in question total and apply
                                            // to array
        System.out.println("Enter the answer key: ");
        for (int i = 0; i < answers.length; i++) {
            answers[i] = scan.nextInt();
        }
        for (int i = 0; i < answers.length; i++) {
            System.out.println("Enter the answer to be graded : ");
            int toGrade = scan.nextInt();
            if (toGrade == answers[i]) {
                count++;
            }
        }
        percentTotal = ((double) count / quizQuest);
        NumberFormat defaultFormat = NumberFormat.getPercentInstance();
        defaultFormat.setMinimumFractionDigits(2);
        System.out.println("The questions answered correctly total: " + count);
        System.out.println("The percentage correct is: " + defaultFormat.format(percentTotal));
        System.out.println("\nAnother quiz to be graded?");
    }
}
// do ( quizQuest != 0){ //condition check to run new quiz against KEY
// for (int j = 0; (quizQuest = scan.nextInt()) != 0; j++); { 

At the bottom is what I had considered for the loop portion I am having trouble with.

3
  • 1
    For checking whether it is even or not use,% and for continous loop until user terminates, search it either on google or stackoverflow, there are many similar questions Commented Oct 28, 2015 at 11:29
  • 1
    SO Community should pin this post as example for HOW TO ASK HOMEWORK QUESTIONS ON SO ;P Commented Oct 28, 2015 at 11:35
  • You can use modulo (%) to get the remainder after division (1 % 2 == 1). With this you can construct a loop with a branch that only does something whenever the condition matches. Alternatively, you can define your loop to increment by 2 each time around rather than 1. It will be worth reading up on both approaches and using the one you think fits best. Commented Oct 28, 2015 at 11:37

3 Answers 3

1

As you asked for hints (and not the code), here it is:

For more than one quizzes, use do-while, as follows:

do{
    //do something
    //scan the value of quiz quest
    //do something
}while(quizquest != 0)

Now, if only answers at even positions are to be checked, do following:

for (int i =0; i <answers.length; i++)
   {

       System.out.println("Enter the answer to be graded : ");
       int  toGrade = scan.nextInt();

       if(i % 2 == 0 && toGrade == answers[i]){   
          count++ ;

       }


   }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! It got the wheels turning! I'll have this spit out in no time now.
Glad it helped. Accept the answer by clicking the green tick :)
1

Create a loop that asks to grade another quiz: You could use a do-while loop with a boolean indicating if the user (teacher?) wants to grade another quiz:

do{
    boolean continue = false; 
    // check if the user wants to continue
while(continue);

Have it only check/score/calculate every other answer. ie: even answers only: You can check for even answers with the modulo operator:

if(i % 2 == 0){
    // even answer
}

Hope this helps!

1 Comment

Thank you! I hadn't considered the modulo operator. I had a do/while placed within before but ran into errors with it only keeping the first grade. So when I tested it with 4 questions half right then did all right both outputs showed a score of 50%. I need to play with placement of the do{
0

You should have a variable, perhaps named continue, whose default value is 'Y'. At the very beginning, create a while loop that checks the condition continue==Y, and at the end when you ask "Another quiz to be graded?", read in the input to the variable continue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.