我正在尝试编写一个执行以下操作的程序。
1)提示用户输入整数并存储它。 2)使用用户输入来计算 *算术平均值 *几何平均数 *他们刚输入的号码的最小值和最大值。
我已经做了大部分工作,从我看到的,我向用户询问输入并将其存储在变量userInput下 第一种方法。
我不确定如何使用userInput进行算术平均值,几何平均值以及最小值和最大值。
public class SimpleStatistics {
public static double[] getUserInput() {
// Returns a string of doubles
Scanner sc = new Scanner(System.in);
// Create list
List<Double> inputList = new ArrayList<Double>();
// Getting input
System.out.println("Please enter number");
double userInput = sc.nextDouble();
// See our new list
System.out.println(inputList);
double arr[] = new double[inputList.size()];
System.out.println(inputList.size());
return arr;
}
public static double arithmeticMean(double[] nums) {
double mean = 0;
double sum = 0;
for (int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
}
mean = sum / nums.length;
return mean;
}
public static double geometricMean(double[] nums) {
double gm = 1.0;
for (int i = 0; i < nums.length; i++) {
gm *= nums[i];
}
gm = Math.pow(gm, 1.0 / (double) nums.length);
return gm;
}
public static double[] minAndmax(double[] nums) {
double min = nums[0];
double max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
} else if (nums[i] > max) {
max = nums[i];
} else {
}
}
double[] minAndmax = { min, max };
return minAndmax;
}
public static double[] scaleUp(double[] nums, int factor) {
for (int i = 0; i < nums.length; i++) {
nums[i] *= factor;
}
return nums;
}
public static double[] scaleDown(double[] nums, int factor) {
for (int i = 0; i < nums.length; i++) {
nums[i] /= factor;
}
return nums;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] input = { 1, 2.8, 5.3, 100, -5, -6.5 };
System.out.println("Choose a option 1-6");
boolean exit = false;
while (!exit) {
System.out.println();
System.out
.println("1) Arithmetic mean, 2) Geometric mean, 3) minAndmax, 4) Scale Up, 5) Scale Down, 6) Quit");
System.out.print("Input -> ");
int choice = sc.nextInt();
System.out.println();
switch (choice) {
case 1: {
// Arithmetic mean
System.out.println("Arithmetic mean");
System.out.println(arithmeticMean(input));
break;
}
case 2: {
// Geometric mean
System.out.println("Geometric mean");
System.out.println(arithmeticMean(input));
break;
}
case 3: {
// Min and max
System.out.println("Min and Max");
for (double i : minAndmax(input)) {
System.out.print(i + ", ");
}
break;
}
case 4: {
// Scale Up
System.out.println("Scale Up");
System.out
.print("Please enter factor by which you want to scale -> ");
int factor = sc.nextInt();
for (double i : scaleUp(input, factor)) {
System.out.print(i + ", ");
}
break;
}
case 5: {
// Scale Down
System.out.println("Scale Down");
System.out
.print("Please enter factor by which you want to scale -> ");
int factor = sc.nextInt();
for (double i : scaleDown(input, factor)) {
System.out.print(i + ", ");
}
break;
}
case 6: {
exit = true;
break;
}
}
}
}
}
答案 0 :(得分:2)
您实际上并没有将数字添加到双数组中。将getUserInput()函数更改为以下内容:
public static double[] getUserInput() {
Scanner sc = new Scanner(System.in);
System.out.println("How many numbers are you entering?");
double[] arr = new double[sc.nextInt()];
for (int i = 0; i < arr.length; i++) {
System.out.println("Please enter your number");
arr[i] = sc.nextDouble();
}
sc.close();
return arr;
}
答案 1 :(得分:1)
将getUserInput
方法更改为:
public static double[] getUserInput() {
Scanner sc = new Scanner(System.in);
List<Double> inputList = new ArrayList<Double>();
System.out.println("Please enter how many numbers you will be inputing");
int numberOfInputs = sc.nextInt();
for (int i = 0; i < numberOfInputs; i++) {
System.out.println("Please enter a number");
double userInput = sc.nextDouble();
inputList.add(userInput);
}
sc.close();
double[] arr = new double[inputList.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = inputList.get(i);
}
return arr;
}
然后,在main方法中,通过调用方法来使用输入。 E.g。
// Arithmetic mean
System.out.println("Arithmetic mean");
System.out.println(arithmeticMean(getUserInput()));
break;
我很乐意提供帮助,请询问是否还有其他事项。