用户提示在表单中输入6个整数(文本字段)。到目前为止,我已经完成了平均值。如何为最大和最小的数字创建代码?
private void averageButtonActionPerformed(java.awt.event.ActionEvent evt) {
int number1 = 0;
int number2 = 0;
int number3 = 0;
int number4 = 0;
int number5 = 0;
int number6 = 0;
try {
number1 = Integer.parseInt(
this.oneTextField1.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad first number", "Error",
JOptionPane.ERROR_MESSAGE);
}
try {
number2 = Integer.parseInt(
this.twoTextField2.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad number 2", "Error",
JOptionPane.ERROR_MESSAGE);
}
try {
number3 = Integer.parseInt(
this.threeTextField3.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad number 3", "Error",
JOptionPane.ERROR_MESSAGE);
}
try {
number4 = Integer.parseInt(
this.fourTextField4.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad number 4", "Error",
JOptionPane.ERROR_MESSAGE);
}
try {
number5 = Integer.parseInt(
this.fiveTextField5.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad number 5", "Error",
JOptionPane.ERROR_MESSAGE);
}
try {
number6 = Integer.parseInt(
this.sixTextField6.getText());
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Bad number 6", "Error",
JOptionPane.ERROR_MESSAGE);
}
int answer = (number1 + number2+number3+number4+number5+number6)/6;
this.answerLabel1.setText("The answer is: " +answer);
答案 0 :(得分:0)
int answer = (number1);
this.answerLabel1.setText("The answer is: " +answer);
if (number1<number2)
answer =(number2);
else if (number2 < number3)
{
answer=number3;
}
this.answerLabel1.setText("The answer is: " +answer);