在java中使用三种方法和一个for循环来寻找平均值 [英] Finding average using three methods and a for loop in java
本文介绍了在java中使用三种方法和一个for循环来寻找平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.util.Scanner;
public class AverageWithMethods {
public static void main(String [] args){
String userInput =;
double avgVal = 0;
// calculateAvg(userInput);
// getUserInput();
printResults(userInput,avgVal);
public static String getUserInput(){
String userInput;
扫描仪scnr =新扫描仪(System.in);
System.out.print(请在一行中输入最多十个数字:);
userInput = scnr.nextLine();
返回userInput;
public static double calculateAvg(String userInput){
double avgVal = 0;
double totSum = 0;
int i;
String newUserInput [] = getUserInput()。split();
double [] userInputArray = new double [newUserInput.length];
for(i = 0; i< newUserInput.length; ++ i){
String userInputString = newUserInput [i];
userInputArray [i] = Double.parseDouble(userInputString);
totSum + = userInputArray [i];
avgVal = totSum / newUserInput.length;
}
return avgVal;
$ b $ public static void printResults(String userInput,double avgVal){
System.out.println(数字的平均值+ getUserInput()+is + calculateAvg(userInput));
}
}
这是我的输出。
请在一行中输入最多10个数字:10 20 30
请在一行中输入最多十个数字:10 20 30
数字10 20 30的平均值是20.0
BUILD SUCCESSFUL(总计时间:6秒)
我只需要知道为什么提示符(请输入最多十个数字在一行中:)打印两次
getUserInput()方法两次:一次在
printResults()
方法,一次在 calculateAvg()
中。您可能想要更改:
String newUserInput [] = getUserInput()。split();
到
String newUserInput [] = userInput.split();
package averagewithmethods;
import java.util.Scanner;
public class AverageWithMethods {
public static void main(String[] args) {
String userInput = "";
double avgVal = 0;
//calculateAvg(userInput);
//getUserInput();
printResults(userInput, avgVal);
}
public static String getUserInput() {
String userInput;
Scanner scnr = new Scanner(System.in);
System.out.print("Please enter up to ten numbers in one line: ");
userInput = scnr.nextLine();
return userInput;
}
public static double calculateAvg(String userInput) {
double avgVal = 0;
double totSum = 0;
int i;
String newUserInput[] = getUserInput().split(" ");
double[] userInputArray = new double[newUserInput.length];
for (i = 0; i < newUserInput.length; ++i) {
String userInputString = newUserInput[i];
userInputArray[i] = Double.parseDouble(userInputString);
totSum +=userInputArray[i];
avgVal = totSum/newUserInput.length;
}
return avgVal;
}
public static void printResults(String userInput, double avgVal) {
System.out.println("The average of the numbers " + getUserInput() + " is " + calculateAvg(userInput));
}
}
This is my output.
Please enter up to ten numbers in one line: 10 20 30
Please enter up to ten numbers in one line: 10 20 30
The average of the numbers 10 20 30 is 20.0
BUILD SUCCESSFUL (total time: 6 seconds)
The only thing I need to know is why does the prompt ("Please enter up to ten numbers in one line: ") print twice
解决方案
You're calling the getUserInput()
method twice: once in the printResults()
method and once in the calculateAvg()
.
You probably want to make the following change:
String newUserInput[] = getUserInput().split(" ");
to
String newUserInput[] = userInput.split(" ");
这篇关于在java中使用三种方法和一个for循环来寻找平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文