gpt4 book ai didi

java - 在java中使用1个函数而不是3个函数

转载 作者:行者123 更新时间:2023-12-02 05:15:21 26 4
gpt4 key购买 nike

下面是我的作业,我已经评分了,不用担心!反馈是“您不需要 3 个不同的函数来读取输入:使用相同的函数 3 次”。有人可以给我看看这个吗?我是 Java 新手,非常想在继续之前了解一下。

/*
*/
package numbers;

/**
*/
import javax.swing.*;
public class Numbers {

public static void main(String[] args) {
//Declare variables
float number1 = 0, number2 = 0, number3 = 0, smallest = 0, largest = 0,
average, output;
int count = 0;
while (count< 3) {
count += 1;

//Call Input method
number1 = firstNumber();
count+=1;
number2 = secondNumber();
count+=1;
number3 = thirdNumber();

//Method to find the largest and smallest numbers
largest = max(number1, number2, number3);
smallest = min(number1, number2, number3);

}
//Methods to calculate average and display output messages
average = mean(number1, number2, number3);
results(largest, smallest, average);

//Warning messages regarding size of input numbers
if (smallest > 50){
JOptionPane.showMessageDialog(null,"WARNING, YOUR NUMBERS ARE ALL "
+ "OVER 50!");
}
if (largest < 50){
JOptionPane.showMessageDialog (null,"WARNING ALL YOUR NUMBERS ARE "
+ "UNDER 50!");
}
}
/*
* Method for Data Input
*/
public static float firstNumber() {
float firstNumber = Float.parseFloat (JOptionPane.showInputDialog("Enter your "
+ "first number: "));
return firstNumber;
}
public static float secondNumber() {
float secondNumber = Float.parseFloat (JOptionPane.showInputDialog("Enter your "
+ "second number: "));
return secondNumber;
}
public static float thirdNumber() {
float thirdNumber = Float.parseFloat (JOptionPane.showInputDialog("Enter your "
+ "third and final number: "));
return thirdNumber;
}

/*
* Method for maximum calculation
*/
public static float max(float firstNumber, float secondNumber, float thirdNumber) {
float maxValue;
if ((firstNumber >= secondNumber) && (firstNumber >= thirdNumber))
maxValue = firstNumber;
else if ((firstNumber <= secondNumber) && (secondNumber >= thirdNumber))
maxValue = secondNumber;
else
maxValue = thirdNumber;
return maxValue;
}
/*
* Method for minimum calculation
*/
public static float min(float firstNumber, float secondNumber, float thirdNumber) {
float minValue;
if ((firstNumber <= secondNumber) && (firstNumber <= thirdNumber))
minValue = firstNumber;
else if ((firstNumber >= secondNumber) && (secondNumber <= thirdNumber))
minValue = secondNumber;
else
minValue = thirdNumber;
return minValue;
}
/*
* Method for average calculation
*/
public static float mean(float firstNumber, float secondNumber, float thirdNumber){
float average;
average = ((firstNumber + secondNumber + thirdNumber)/3);
return average;
}
/*
* Method for display
*/
public static void results(float maxValue, float minValue, float average){
JOptionPane.showMessageDialog(null,"The largest of your input "
+ "numbers is: " + maxValue+ ". " + "The smallest of your input "
+ "numbers is: " + minValue+". " + "And, the average of your input "
+ "numbers is: " +average);
}

}

谢谢!

最佳答案

您可以将所有“获取数字”方法折叠为采用参数的方法。

public static float getNumber(String promptText) {

return Float.parseFloat(JOptionPane.showInputDialog(promptText));
}

使用它,您只需将其放入 main 方法中即可。

float number1 = getNumber("Enter your First Number");

关于java - 在java中使用1个函数而不是3个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27003229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com