我的程序有问题

时间:2014-11-26 10:51:13

标签: java

代码存在一些问题,想知道是否有人可以提供帮助。目前即将出现的问题是,当你运行程序时,它会使计数器计数正常,但它并没有将数字加在一起作为总和,所以没有总和,平均值将不起作用。我正在使用Eclipse。

import java.awt.*;
import java.awt.event.*;
import java.beans.*;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import java.util.ArrayList;
import java.util.Scanner;

class GPA extends JDialog
        implements ActionListener,
        PropertyChangeListener {
	

    private String typedText = null;
    private JTextField textField;
    private String magicWord;
    private JOptionPane optionPane;
    private String btnString1 = "Next Grade";
    private String btnString2 = "Average";
    private String btnString3 = "Exit";
    ArrayList<Integer> grades = new ArrayList<Integer>();
    int sum = 0;
    int gradeCounter = 0;
    int average = 0;

    /**
     * Returns null if the typed string was invalid; otherwise, returns the
     * string as the user entered it.
     */
    public String getValidatedText() {
        return typedText;
    }

    /**
     * Creates the reusable dialog.
     */
    public GPA(Frame aFrame, String aWord) {
        super(aFrame, true);

        magicWord = aWord.toUpperCase();
        setTitle("Average Grades");

        textField = new JTextField(10);

        //Create an array of the text and components to be displayed.
        String msgString1 = "Please enter a grade (0-100)";
        Object[] array = {msgString1, textField};

        //Create an array specifying the number of dialog buttons
        //and their text.
        Object[] options = {btnString1, btnString2, btnString3,};

        //Create the JOptionPane.
        optionPane = new JOptionPane(array,
                JOptionPane.QUESTION_MESSAGE,
                JOptionPane.YES_NO_CANCEL_OPTION,
                null,
                options,
                options[0]);

        //Make this dialog display it.
        setContentPane(optionPane);

        //Handle window closing correctly.
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        //Ensure the text field always gets the first focus.
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent ce) {
                textField.requestFocusInWindow();
            }
        });

        //Register an event handler that puts the text into the option pane.
        textField.addActionListener(this);

        //Register an event handler that reacts to option pane state changes.
        optionPane.addPropertyChangeListener(this);
        pack();
    }

    /**
     * This method handles events for the text field.
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        optionPane.setValue(btnString1);
    }

    /**
     * This method reacts to state changes in the option pane.
     */
    @Override
    public void propertyChange(PropertyChangeEvent e) {
        String prop = e.getPropertyName();

        if (isVisible()
                && (e.getSource() == optionPane)
                && (JOptionPane.VALUE_PROPERTY.equals(prop)
                || JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
            Object value = optionPane.getValue();

            if (value == JOptionPane.UNINITIALIZED_VALUE) {
                //ignore reset
                return;
            }

            //Reset the JOptionPane's value.
            //If you don't do this, then if the user
            //presses the same button next time, no
            //property change event will be fired.
            optionPane.setValue(
                    JOptionPane.UNINITIALIZED_VALUE);

            if (btnString1.equals(value)) {
            int text = Integer.parseInt(textField.getText());
                grades.add(text);
                textField.setText("");
                gradeCounter = gradeCounter ++ ;
                
                if (btnString2.equals(value)) {
                for (int i = 0; i < grades.size(); ++i) {
                   sum = grades.get (i) + grades.get(i++);
  
                }
                average = sum / gradeCounter;
                    JOptionPane.showMessageDialog(this, sum);
                  
                    exit();
                }
        }else { //user closed dialog or clicked cancel
        JOptionPane.showMessageDialog(this, sum);
                
                typedText = null;
                exit();
            }
        }
}

    /**
     * This method clears the dialog and hides it.
     */
    public void exit() {
        dispose();
    }

    public static void main(String... args) {
        //create JDialog and components on EDT
        SwingUtilities.invokeLater(new Runnable() {
            
            public void run() {
                new GPA(null, "").setVisible(true);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

这里有一些小问题导致你出现问题。

if (btnString1.equals(value)) {没有相应的else因此,当按下第二个按钮时,甚至不会调用btnString2

其次JOptionPane.showMessageDialog(this, average);应为JOptionPane.showMessageDialog(this, sum);目前只显示总和而不是平均值。

最后gradeCounter = gradeCounter ++ ;可以设置为gradeCounter ++ ;因为它是gradeCounter不会增加。 ++运算符将递增计数器,而无需使用=再次设置它。

平均计算也存在错误,其中不需要sum = grades.get(i) + grades.get(i++)。由于围绕它有一个for语句,它将循环遍历列表中的所有等级。最好只说sum += grades.get(i);

以下是已更改的新代码部分。

if (btnString1.equals(value)) {
    int text = Integer.parseInt(textField.getText());
    grades.add(text);
    textField.setText("");
    gradeCounter ++ ;
} else if (btnString2.equals(value)) {
    for (int i = 0; i < grades.size(); ++i) {
       sum += grades.get (i);
    }
    average = sum / gradeCounter;
    JOptionPane.showMessageDialog(this, average);

    exit();

}