0

Here's what I need to do: Ask the user how many quizzes he took. Then, prompt the user for that many grades. When the user is done entering the grades, tell him his average grade for all his quizzes. You don't know what number the user will enter

For example, if the user says he took 3 quizzes, the prompts should say: "Enter quiz grade 1: ", then "Enter quiz grade 2: ", then "Enter quiz grade 3: "

This is as far I've gotten..I'm not sure how to prompt the user multiple times depending on how many quizzes the user took...

int numQuiz;
count = 1;
numQuiz = Integer.parseInt(JOptionPane.showInputDialog("How many quizzes did you take?"));
do
{

} while (count != 0);
Delimitry
  • 2,987
  • 4
  • 30
  • 39
Dan
  • 55
  • 1
  • 7

2 Answers2

2

a for-loop might help:

for (int i = 0; i < numQuiz; i++) {
    // ask user to "Enter quiz grade XX", XX being (i+1)
} 
1

Why not you simply ask the user for, "How many quiz did you take?". After receiving this input from the user, simply create a new JPanel based on the input specified by the user and then present this new JPanel inside a JOptionPane to do that in one step, instead of asking multiple times and annoying the user with multiple JOptionPanes.

A small example :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class OptionPaneTest {

    private int size;
    private JTextField[] gradeField;

    private void displayGUI() {
        size = Integer.parseInt(
            JOptionPane.showInputDialog(
                "Enter total grades to calculate : "));
        System.out.println(size);

        JOptionPane.showMessageDialog(null, getPanel(size));
    }

    private JPanel getPanel(final int size) {
        JPanel panel = new JPanel(
            new FlowLayout(FlowLayout.LEFT, 5, 5));
        JLabel label = new JLabel("Average : ", JLabel.CENTER);
        panel.add(label);
        gradeField = new JTextField[size];
        for (int i = 0; i < gradeField.length; i++) {
            gradeField[i] = new JTextField(3);          
            panel.add(gradeField[i]);
        }

        gradeField[size - 1].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                int sum = 0;
                for (JTextField textField : gradeField) {
                    if (textField.getDocument().getLength() > 0) {
                        sum += Integer.parseInt(textField.getText());
                    }
                }
                System.out.println("Average : " + (sum / size));
            }
        });

        return panel;
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new OptionPaneTest().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

Hope you can modify it, for your needs. It gives the output on the console. Or else you can place a JLabel on this same JPanel to show the Average to the user.

But, the thingy that surprises me, is the fact, that the program is asking the user to put his/her grades. Will the user be that genuine to provide (or will remember all the grades collected in the past). It would be wise, if you put each grade earned by the user in a List or some other Collection and simply iterate over it, to find the Average (though it is just my opinion, since I really don't know much about your project)

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • I can't. We haven't covered that in class yet. So far we've only covered while, do-while, and for loops – Dan Sep 27 '13 at 17:36
  • @Dan : What you cann't? This example, has nothing that you can call advance. Though What you referring to, is just the basic Core Java part, then how come your instructor, put you to `Swing`, which might will have far more advance topics for you to handle at this early stages of your learning process :-) I wanted to refer you [this link](http://stackoverflow.com/a/18168428/1057230), which is using `DocumentListener` so that you can calculate __Average__ straight away as the user types in any `JTextField`, but now I am thinking I did the right thingy :-) – nIcE cOw Sep 27 '13 at 18:03