gpt4 book ai didi

java - 存储数组中的数据以供以后比较

转载 作者:行者123 更新时间:2023-11-29 04:13:24 24 4
gpt4 key购买 nike

我有一个程序,用户可以输入 4 个数字。输入所有数字后,将为用户提供四个值中的平均值、最低值和最高值。如果用户继续该程序,它会要求再输入 4 个值,并再次给出这四个值中的平均值、最低值和最高值。 (等等……)

但是,我似乎无法弄清楚如何比较给出的数组结果,以输出最低的最低值、最高的最高值以及每个 4 位数组的所有结果中给出的所有结果的平均值。

如果我需要更多解释,请告诉我。

*代码不完整

package example;

import java.util.*;

public class Example {

double highestValue;
double lowestValue;
public static void main(String[] args) {

System.out.println("Hello (Enter each digit when prompted)");
System.out.println();

Scanner input = new Scanner(System.in);

String yesOrNo = "y";
while (yesOrNo.equalsIgnoreCase("y")) {
// Calls inputDigit and stores array in array storage variable
double[] array = inputDigit(input, "Enter a number: ");

displayFirst(array);
highestChecker(array);

System.out.println();

yesOrNo = askToContinue(input);
System.out.println();

System.out.println();

if (yesOrNo.equalsIgnoreCase("n")) {
System.out.println("done!);
displayHighest(array);
}
}
}

public static double[] inputDigit(Scanner input, String prompt) {
// Creates a 4 spaced array
double[] array = new double[4];
// For loop that stores each input by user
for (int counter = 0; counter < array.length; counter++) {
System.out.print(prompt);

try {
array[counter] = input.nextDouble();
if (array[counter] <= 1000){
} else if (array[counter] >= -100){
} else {
System.out.println("Error!\nEnter a number greater or equal to -100 and"
+ "less or equal to 1000.");
}
} catch (InputMismatchException e){
System.out.println("Error! Please enter a digit.");
counter--; // This is designed to backup the counter so the correct variable can be input into the array
input.next();
}
}
return array;
}

public static void displayFirst(double[] array){
Arrays.sort(array);
double highestValue = array[3];
double lowestValue = array[0];
double average = calculateAverage(array);
System.out.println("RESULTS FOR CURRENT NUMBER CYCLE"
+ "\n--------------------------------"
+ "\nAverage Value - " + average
+ "\nHighest Value - " + highestValue
+ "\nLowest Value - " + lowestValue);
}

public static double[] highestChecker(double[] array){
double[] arrayC = Arrays.copyOf(array, array.length);
Arrays.sort(arrayC);
double lowestChecked = arrayC[0];
double highestChecked = arrayC[3];
double highestAverage;
// Check lowest value

// Check highest value
return array;
}

public static void displayHighest(double[] array){
Arrays.sort(array);
double highestValue = array[0];
double lowestValue = array[3];
double average = calculateAverage(array);
System.out.println("RESULTS FOR HIGHEST VALUE"
+ "\n-------------------------"
+ "\nHighest Average Value - " + average
+ "\nHighest Highest Value - " + highestValue
+ "\nHighest Lowest Value - " + lowestValue);
}

public static double calculateAverage(double[] array) {
double sum = 0;
double average;
for (int i = 0; i < array.length; i++) {
sum = sum + array[i];
}
average = (double)sum/array.length;
return average;
}

public static String askToContinue(Scanner input) {
boolean loop = true;
String choice;
System.out.print("Continue? (y/n): ");
do {
choice = input.next();
if (choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n")) {
System.out.println();
loop = false;
} else {
System.out.print("Please type 'Y' or 'N': ");
}
} while (loop);
return choice;
}
}

程序应该做什么的示例输出。

Enter a number: 2
Enter a number: 4
Enter a number: 6
Enter a number: 8
RESULTS FOR CURRENT NUMBER CYCLE
--------------------------------
Average Value - 5.0 // <-- This value should the highest average
Highest Value - 8.0
Lowest Value - 2.0

Continue? (y/n): y


Enter a number: 7
Enter a number: 5
Enter a number: 3
Enter a number: 1
RESULTS FOR CURRENT NUMBER CYCLE
--------------------------------
Average Value - 4.0
Highest Value - 7.0
Lowest Value - 1.0 // <-- This value should be the lowest, lowest value

Continue? (y/n): y


Enter a number: 9
Enter a number: 1
Enter a number: 4
Enter a number: 5
RESULTS FOR CURRENT NUMBER CYCLE
--------------------------------
Average Value - 4.75
Highest Value - 9.0 // <-- This value should be the highest, highest value
Lowest Value - 1.0 // <-- This value should be the lowest, lowest value

Continue? (y/n): n

You have exited the program.
Thank you for your time.

这应该是最终结果。

RESULTS FOR HIGHEST VALUE
-------------------------
Highest Average Value - 5.0
Highest, Highest Value - 9.0
Lowest, Lowest Value - 1.0

最佳答案

要找到平均值,您可以使用 java-8

OptionalDouble average = Arrays.stream(array).average();

要找到最低和最高的元素,我们可以对其进行排序并使用:

// sort the array (default by ascending)
Arrays.sort(array);
double lowest = array[0];
double highest = array[array.length - 1];

由于数组已排序,第一个索引应该是最低 元素,最后一个是最高


编辑:

要找到整体的最低值和最高值,您可以定义两个静态变量:

static double highestValueOverAll = Double.MIN_VALUE;
static double lowestValueOverAll = Double.MAX_VALUE;

现在在 highestChecker() 中(可能重命名)

public static double[] highestChecker(double[] array) {

// ...

if (lowestChecked < lowestValueOverAll)
lowestValueOverAll = lowestChecked;

if (highestChecked > highestValueOverAll)
highestValueOverAll = highestChecked;

// ...
}

如果当前的最低/最高值小于/大于之前的值,我们只是进行简单的交换。 while-loop 完成后,您可以将其打印出来。

注意:数组也是对象,因此您不需要在每个方法中对它们进行排序

关于java - 存储数组中的数据以供以后比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53763908/

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