2012-10-10 33 views
5

編寫一個程序,該程序使用輸入對話框讀取三個測試標記,每個標記爲100個。程序丟棄最低標記並在消息對話框中顯示兩個較高標記的平均值。JOptionPane - 輸入對話框程序

這是我多遠了,我不知道從哪裏這裏做,任何幫助,將不勝感激:

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

    } 

} 
+0

參見[這些提示](http://home.earthlink.net/~patricia_shanahan/beginner.html),讓你開始 - 如果不是一個解決方案,然後在至少到達要求特定問題的階段。 –

+0

100,000個意見對此問題/答案。我今天用它。然而,它因爲過於本土化而關閉。哈哈,好的。 –

+0

僅供參考 - 當搜索'java swing input alert'的組合時,此問題爲#2結果 –

回答

2

之後,你要解析的結果。 假設結果是整數,然後

int testint1 = Integer.parse(test1); 

同樣其他應分析。 現在應使用if語句 檢查結果中兩個更高的分數,然後取出平均值。

+0

如果有3個數字,我將如何編寫if語句,因爲您無法編寫if(test3 user1733283

+0

你可以,如果(test3 greatmajestics

+0

if i不想使用&&我怎麼能做這個問題? – user1733283

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

    } 

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

爲什麼用三種不同的對話框惹惱用戶輸入的事情,爲什麼不去做這一切一氣呵成在單個對話框中節省時間,而不是測試用戶的耐心?

通過將所有字段放在您的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(); 
      } 
     }); 
    } 
}