Dan
Dan

Reputation: 55

Mutiple Dialog Box Prompts Depending On User Input

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);

Upvotes: 0

Views: 465

Answers (2)

nIcE cOw
nIcE cOw

Reputation: 24626

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)

Upvotes: 1

user180100
user180100

Reputation:

a for-loop might help:

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

Upvotes: 2

Related Questions