平均等级:获得0而不是用户输入

时间:2014-03-19 16:18:39

标签: arrays sorting while-loop containers jtextpane

我的代码几近完美......我想。我需要输入用户输入的测试分数(最多50分)然后当用户输入-1时,它将显示他们输入的所有分数从最低到最高排序,然后在列表的底部排序说“平均值 _ _”。不幸的是,当我运行它时,我得到一个0的大量列表,我无法弄清楚我哪里出错了。如果有人有任何想法我会很感激

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

public class AverageGrades extends JFrame
{
//construct conponents
static JLabel title = new JLabel("Average of Grades");
static JTextPane textPane = new JTextPane();
static int numberOfGrades = 0;
static int total = 0;
static DecimalFormat twoDigits = new DecimalFormat ("##0.00");

//set array
static int[] grades = new int[50];

//create content pane
public Container createContentPane()
{
    //create JTextPane and center panel
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new FlowLayout());
    northPanel.add(title);

    JPanel centerPanel = new JPanel();
    textPane = addTextToPane();
    JScrollPane scrollPane = new JScrollPane(textPane);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(500,200));
    centerPanel.add(scrollPane);

    //create Container and set attributes
    Container c = getContentPane();
        c.setLayout(new BorderLayout(10,10));
        c.add(northPanel,BorderLayout.NORTH);
        c.add(centerPanel,BorderLayout.CENTER);

    return c;
}

//method to add new text to JTextPane
public static JTextPane addTextToPane()
{
    Document doc = textPane.getDocument();
    try
    {
        // clear previous text
        doc.remove(0,doc.getLength());

        //insert title
        doc.insertString(0,"Grades\n",textPane.getStyle("large"));

        //insert grades and calculate average
        for(int j=0; j<grades.length; j++)
        {
            doc.insertString(doc.getLength(), grades[j] + "\n", textPane.getStyle("large"));
        }
    }
    catch(BadLocationException ble)
    {
        System.err.println("Couldn't insert text");
    }

    return textPane;
}

//method to sort array
public void grades(int grdArray[])
{
    //sort int array
    for (int pass = 1; pass<grdArray.length; pass++)
    {
        for (int element = 0; element<grdArray.length -1; element++)
        {
            swap(grades, element, element + 1);

        }
    }
        addTextToPane();

}


//method to swap elements of array
public void swap(int swapArray[], int first, int second)
{
    int hold;
    hold = swapArray[first];
    swapArray[first] = swapArray[second];
    swapArray[second] = hold;
}

//execute method at run time
public static void main(String args[])
{
    JFrame.setDefaultLookAndFeelDecorated(true);
    AverageGrades f = new AverageGrades();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    //accept first grade
    int integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a grade (0-100) or -1 to calculate the average"));

    //while loop accepts more grades, keeps count, and calulates the total
    int count = 0;
    int[] grades = new int[50];
    int num = 0;
    while (count<50 && num!= -1)
    {
        num = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a grade (0-100) or -1 to calculate the average" + (count+1)));
        if(num!=-1)
            grades[count] = num;
        count++;

    }

    //create content pane
    f.setContentPane(f.createContentPane());
    f.setSize(600,375);
    f.setVisible(true);


}

}

2 个答案:

答案 0 :(得分:1)

您定义了grade array两次:一次在class内:

static int[] grades = new int[50];

然后在main()

int[] grades = new int[50];

答案 1 :(得分:0)

以下是解决您问题的更正代码。

有两个主要问题,您使用的是本地数组成绩而不是静态全局数组成绩。 (所以从main中删除声明)

其次,你永远不会插入导致第一个元素丢失的第一个值integerInput。

       import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import java.text.DecimalFormat;

    class AverageGrades extends JFrame {
        // construct conponents
        static JLabel title = new JLabel("Average of Grades");
        static JTextPane textPane = new JTextPane();
        static int numberOfGrades = 0;
        static int total = 0;
        static DecimalFormat twoDigits = new DecimalFormat("##0.00");

        // set array
        static int[] grades = new int[50];

        // create content pane
        public Container createContentPane() {
            // create JTextPane and center panel
            JPanel northPanel = new JPanel();
            northPanel.setLayout(new FlowLayout());
            northPanel.add(title);

            JPanel centerPanel = new JPanel();
            textPane = addTextToPane();
            JScrollPane scrollPane = new JScrollPane(textPane);
            scrollPane
                    .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scrollPane.setPreferredSize(new Dimension(500, 200));
            centerPanel.add(scrollPane);

            // create Container and set attributes
            Container c = getContentPane();
            c.setLayout(new BorderLayout(10, 10));
            c.add(northPanel, BorderLayout.NORTH);
            c.add(centerPanel, BorderLayout.CENTER);

            return c;
        }

        // method to add new text to JTextPane
        public static JTextPane addTextToPane() {
            int counter = 0;
            Document doc = textPane.getDocument();
            try {
                // clear previous text
                doc.remove(0, doc.getLength());

                // insert title
                doc.insertString(0, "Grades\n", textPane.getStyle("large"));

//BREAKING IF NUMBER IS 0, SIGNIFIES THAT ALL ELEMENTS ARE COVERED
//counter IS USED TO CALCULATE THE TOTAL NUMBER OF ELEMENTS
                // insert grades and calculate average
                for (int j = 0; j < grades.length; j++) {
                    if (grades[j] != 0) {
                        doc.insertString(doc.getLength(), grades[j] + "\n",
                                textPane.getStyle("large"));
                        counter++;
                    } else {
                        break;
                    }
                }

//THIS LINE INSERTS THE AVERAGE IN THE PANEL
                doc.insertString(doc.getLength(), "Average is :"
                        + (total / counter) + "\n", textPane.getStyle("large"));

            } catch (BadLocationException ble) {
                System.err.println("Couldn't insert text");
            }

            return textPane;
        }


//CORRECT THE SORTING FUNCTION
// THE SORTING SHOULD BE IN REVERSE ORDER FOR YOUR REQUIREMENTS

        // method to sort array
        public void grades(int grdArray[]) {
            // sort int array
            for (int pass = 1; pass < grdArray.length; pass++) {
                for (int element = 0; element < grdArray.length - 1; element++) {
                    swap(grades, element, element + 1);

                }
            }
            addTextToPane();

        }


        // method to swap elements of array
        public void swap(int swapArray[], int first, int second) {
            int hold;
            hold = swapArray[first];
            swapArray[first] = swapArray[second];
            swapArray[second] = hold;
        }

        // execute method at run time
        public static void main(String args[]) {
            JFrame.setDefaultLookAndFeelDecorated(true);
            AverageGrades f = new AverageGrades();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // accept first grade
            int integerInput = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "Please enter a grade (0-100) or -1 to calculate the average"));

            // while loop accepts more grades, keeps count, and calulates the total
            int count = 0;
            grades[0] = integerInput;
            count++;
            int num = 0;
            while (count < 50 && num != -1) {
                num = Integer.parseInt(JOptionPane.showInputDialog(null,
                        "Please enter a grade (0-100) or -1 to calculate the average"
                                + (count + 1)));

//USING TOTAL FIELD TO CALCULATE SUM
                if (num != -1) {
                    grades[count] = num;
                    total += grades[count];
                }



                count++;

            }

//CALL THE SORTING FUNCTION AFTER CORRECTING IT

            //f.grades(grades);

            // create content pane
            f.setContentPane(f.createContentPane());
            f.setSize(600, 375);
            f.setVisible(true);

        }
    }