📄 最大值最小值平均值.java
字号:
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class opp5 extends Applet implements ActionListener
{
TextField input1;
TextField input2;
TextField output1;
TextField output2;
TextField output3;
int DataArray[] = new int[100];
int DataInputed = 0;
int max = 0;
int min = 0;
double total = 0;
double avg = 0;
String inputDatas2String = "";
public void init()
{
Label prompt1 = new Label("Please input a number which you want to differ:");
Label prompt2 = new Label("The data your have inputted are: ");
input1 = new TextField(10);
input2 = new TextField(50);
input2.setEditable(false);
output1 = new TextField(30);
output2 = new TextField(30);
output3 = new TextField(30);
add(prompt1);
add(input1);
add(prompt2);
add(input2);
add(new Label("The max number is:"));
add(output1);
add(new Label("The min number is:"));
add(output2);
add(new Label("The avg number is:"));
add(output3);
input1.addActionListener(this);
this.setSize(new Dimension(380, 180));
}
public void actionPerformed(ActionEvent e)
{
/*
* when user press the key "Enter" in the input1 textField, occur an
* ActionEvent object, then this method will execute.
*/
if (e.getSource() == input1)
{
int inputData=0;
try
{
inputData = Integer.parseInt(input1.getText());
} catch (NumberFormatException e1)
{
input2.setText("you have input an invalid data, please re-enter a number!");
input1.selectAll();
//return;
}
DataArray[ DataInputed++] = inputData;
if(DataInputed ==1)
{
max = DataArray[0];
min = DataArray[0];
}
// display the input data in the input2 textfield.
inputDatas2String = inputDatas2String + input1.getText() +" , ";
input2.setText(inputDatas2String);
input1.selectAll();
// find max && min
for (int i = 0; i < DataInputed; i++)
{
if (DataArray[i] >= max)
{
max = DataArray[i];
}
if (DataArray[i] <= min)
{
min = DataArray[i];
}
}
// find avg
total=0;
for (int i = 0; i < DataInputed; i++)
{
total = total + DataArray[i];
avg = total / DataInputed;
}
//avg = total / DataInputed;
output1.setText(max+"");
output2.setText( min+"");
output3.setText( avg+"");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -