Dati 4 reali, è possibile calcolare minimo, massimo, totale, media oppure
uscire
import java.awt.*;
import java.awt.color.*;
import java.awt.event.*;
class MMTMU extends Frame
implements ActionListener
{
private TextField txt1, txt2, txt3, txt4;
private Label lOut;
public MMTMU()
{
Panel pNORTH=new Panel();
txt1=new TextField("-20", 10); pNORTH.add(txt1);
txt2=new TextField("-1" , 10); pNORTH.add(txt2);
txt3=new TextField("+1" , 10); pNORTH.add(txt3);
txt4=new TextField("+20", 10); pNORTH.add(txt4);
this.add(pNORTH, BorderLayout.NORTH);
lOut =new Label("???", Label.CENTER);
this.add(lOut, BorderLayout.CENTER);
Panel pSOUTH=new Panel();
Button bMIN=new Button("Minimo"); bMIN.addActionListener(this); pSOUTH.add(bMIN);
Button bMAX=new Button("Massimo"); bMAX.addActionListener(this); pSOUTH.add(bMAX);
Button bTOT=new Button("Totale"); bTOT.addActionListener(this); pSOUTH.add(bTOT);
Button bMED=new Button("Media"); bMED.addActionListener(this); pSOUTH.add(bMED);
Button bESC=new Button("Uscita"); bESC.addActionListener(this); pSOUTH.add(bESC);
bESC.setBackground(Color.black); bESC.setForeground(Color.white);
this.add(pSOUTH, BorderLayout.SOUTH);
this.setTitle("Calcolo minimo/massimo/totale/media");
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent evento)
{
String nome=evento.getActionCommand();
if(nome.equals("Uscita"))
System.exit(0);
String msg=new String();
try
{
double in1=Double.parseDouble(txt1.getText());
double in2=Double.parseDouble(txt2.getText());
double in3=Double.parseDouble(txt3.getText());
double in4=Double.parseDouble(txt4.getText());
double risultato=0;
if(nome.equals("Minimo"))
{
risultato=Math.min(Math.min(in1, in2), Math.min(in3, in4));
msg="Minimo = " + risultato;
}
else if(nome.equals("Massimo"))
{
risultato=Math.max(Math.max(in1, in2), Math.max(in3, in4));
msg="Massimo = " + risultato;
}
else if(nome.equals("Totale"))
{
risultato=in1+in2+in3+in4;
msg="Totale = "+risultato;
}
else if(nome.equals("Media"))
{
risultato=(in1+in2+in3+in4)/4.0;
msg="Media = "+risultato;
}
}
catch(Exception eccezione)
{
msg="???";
}
lOut.setText(msg);
}
public static void main(String[] args)
{
MMTMU f = new MMTMU();
}
}
Osserva
-
System.exit(0);
|