13

编写一个程序,使用输入对话框读取三个测试分数,每个分数都为 100。该程序会丢弃您的最低分数,并在消息对话框中显示两个较高分数的平均值。

这是我走了多远,我不知道从哪里开始,任何帮助将不胜感激:

import javax.swing.JOptionPane; 

public class Average {

    public static void main (String [] args){

        String test1, test2, test3, avg;

        test1= JOptionPane.showInputDialog("Please input mark for test 1: ");

        test2= JOptionPane.showInputDialog("Please input mark for test 2: ");

        test3= JOptionPane.showInputDialog("Please input mark for test 3: ");

    }

}
4

4 回答 4

16
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class Average {

    public static void main(String [] args) {

        String test1= JOptionPane.showInputDialog("Please input mark for test 1: ");

        String test2= JOptionPane.showInputDialog("Please input mark for test 2: ");

        String test3= JOptionPane.showInputDialog("Please input mark for test 3: ");

        int int1 = Integer.parseInt(test1);
        int int2 = Integer.parseInt(test2);
        int int3 = Integer.parseInt(test3);

        SortedSet<Integer> set = new TreeSet<>();
        set.add(int1);
        set.add(int2);
        set.add(int3);

        Integer [] intArray = set.toArray(new Integer[3]);
        JFrame frame = new JFrame();
        JOptionPane.showInternalMessageDialog(frame.getContentPane(), String.format("Result %f", (intArray[1] + intArray[2]) / 2.0));

    }

}
于 2012-10-10T00:59:36.727 回答
4

为什么要用三个不同的对话框来输入东西来惹恼用户,为什么不在一个对话框中一次性完成所有这些并节省时间,而不是测试用户的耐心呢?

您可以在单个对话框中添加所有内容,方法是将所有字段放在您的JPanel,然后将其添加JPanel到您的JOptionPane. 下面的代码可以澄清一点:

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

public class AverageExample
{
    private double[] marks;
    private JTextField[] marksField;
    private JLabel resultLabel;

    public AverageExample()
    {
        marks = new double[3];
        marksField = new JTextField[3];
        marksField[0] = new JTextField(10);
        marksField[1] = new JTextField(10);
        marksField[2] = new JTextField(10);
    }

    private void displayGUI()
    {
        int selection = JOptionPane.showConfirmDialog(
                null, getPanel(), "Input Form : "
                                , JOptionPane.OK_CANCEL_OPTION
                                , JOptionPane.PLAIN_MESSAGE);

        if (selection == JOptionPane.OK_OPTION) 
        {
            for ( int i = 0; i < 3; i++)
            {
                marks[i] = Double.valueOf(marksField[i].getText());             
            }
            Arrays.sort(marks);
            double average = (marks[1] + marks[2]) / 2.0;
            JOptionPane.showMessageDialog(null
                    , "Average is : " + Double.toString(average)
                    , "Average : "
                    , JOptionPane.PLAIN_MESSAGE);
        }
        else if (selection == JOptionPane.CANCEL_OPTION)
        {
            // Do something here.
        }
    }

    private JPanel getPanel()
    {
        JPanel basePanel = new JPanel();
        //basePanel.setLayout(new BorderLayout(5, 5));
        basePanel.setOpaque(true);
        basePanel.setBackground(Color.BLUE.darker());

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(3, 2, 5, 5));
        centerPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        centerPanel.setOpaque(true);
        centerPanel.setBackground(Color.WHITE);

        JLabel mLabel1 = new JLabel("Enter Marks 1 : ");
        JLabel mLabel2 = new JLabel("Enter Marks 2 : ");
        JLabel mLabel3 = new JLabel("Enter Marks 3 : ");

        centerPanel.add(mLabel1);
        centerPanel.add(marksField[0]);
        centerPanel.add(mLabel2);
        centerPanel.add(marksField[1]);
        centerPanel.add(mLabel3);
        centerPanel.add(marksField[2]);

        basePanel.add(centerPanel);

        return basePanel;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new AverageExample().displayGUI();
            }
        });
    }
}
于 2012-10-10T03:52:10.457 回答
3

一种解决方案是实际使用整数数组而不是单独的test字符串:

您可以将响应循环解析为JOptionPane.showInputDialog数组的各个元素。

Arrays.sort可用于对它们进行排序以允许您挑选出 2 个最高值。

然后可以通过将这 2 个值相加并除以 2 轻松计算平均值。

int[] testScore = new int[3];

for (int i = 0; i < testScore.length; i++) {
   testScore[i] = Integer.parseInt(JOptionPane.showInputDialog("Please input mark for test " + i + ": "));
}

Arrays.sort(testScore);
System.out.println("Average: " + (testScore[1] + testScore[2])/2.0);
于 2012-10-10T01:08:32.480 回答
2

之后,您必须解析结果。假设结果是整数,那么

int testint1 = Integer.parse(test1);

同样,其他的也应该被解析。现在应该通过使用 if 语句检查结果中是否有两个较高的分数,然后取出平均值。

于 2012-10-10T00:58:16.007 回答