2012-10-10 17 views
5

Scrivere un programma che utilizzi finestre di dialogo di input per leggere tre segni di prova, ciascuno su 100. Il programma scarta il segno più basso e mostra la media dei due segni più in alto in una finestra di messaggio.JOptionPane - programma di dialogo di input

Questo è quanto ho ottenuto e io non so dove a fare da qui, qualsiasi aiuto sarebbe apprezzato:

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

Sede [questi suggerimenti] (http://home.earthlink.net/~patricia_shanahan/beginner.html) per iniziare - se non a una soluzione, poi a almeno per arrivare allo stadio di porre una domanda * specifica *. –

+0

100.000 visualizzazioni per questa domanda/risposta. E l'ho usato oggi. Eppure è chiuso per essere troppo localizzato. Ahah ok. –

+0

FYI - Questa domanda è il n. 2 risultato durante la ricerca di combinazioni di 'java swing input alert' –

risposta

2

Dopo di che è necessario analizzare i risultati. risultati Supponiamo che sono in numeri interi, quindi

int testint1 = Integer.parse(test1); 

Allo stesso modo gli altri dovrebbe essere analizzato. Ora i risultati dovrebbero essere controllati per due segni più in alto, usando l'istruzione Dopo aver eliminato la media.

+0

come scriverei l'istruzione if quando ci sono 3 numeri perché non puoi scrivere se (test3 user1733283

+0

È possibile, se (test3 greatmajestics

+0

se non volevo usare il && in quale altro modo potevo fare questa domanda? – 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

Una soluzione è utilizzare effettivamente un array intero anziché separati test stringhe:

Si potrebbe ciclo analizzare la risposta JOptionPane.showInputDialog nei singoli elementi della matrice.

Arrays.sort potrebbe essere utilizzato per ordinarli per consentire di selezionare i 2 valori più alti.

La media può essere facilmente calcolata poi con l'aggiunta di questi 2 valori & dividendo per 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

Perché infastidire l'utente con tre diverse finestre di dialogo per entrare le cose, perché non fare tutto questo in una volta sola in una sola finestra di dialogo e risparmia tempo, invece di testare la pazienza dell'UTENTE?

È possibile aggiungere tutto in una singola finestra di dialogo, inserendo tutti i campi nel proprio JPanel e aggiungendo questo JPanel al numero JOptionPane. Sotto codice può chiarire un po 'di più:

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(); 
      } 
     }); 
    } 
} 
Problemi correlati