我有一个程序,用户输入4个数字。输入所有数字后,将为用户提供四个值中的平均值,最低值和最高值。如果用户继续该程序,它将要求再提供4个值,并再次提供这四个值中的平均值,最低值和最高值。 (依此类推...)
但是,我似乎无法弄清楚如何比较给出的数组结果,以输出每个4位数组的所有结果中给出的所有结果中的最低,最低,最高和平均值。 / p>
如果需要更多说明,请告诉我。
*代码不完整
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
答案 0 :(得分:2)
要找到平均值,您可以使用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];
由于数组已排序,因此第一个索引应该是 lowest 元素,最后一个索引是 highest
编辑:
要查找总体的最低和最高值,可以定义两个静态变量:
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
完成后,您可以将其打印出来。
注意:
数组也是对象,因此您不需要在每种方法中sort
都使用它们。
答案 1 :(得分:1)
通过阅读您的问题描述以及您在打印public static int ordinalIndexOf(String str, String substr, int n) {
int pos = str.indexOf(substr);
while (--n > 0 && pos != -1)
pos = str.indexOf(substr, pos + 1);
return pos;
}
,highestValue
和lowestValue
到目前为止进行的总体计算时遇到的困难,我所了解的内容。
为此,首先在开始时将highestAverage
,highestValue
和lowestValue
变量声明为highestAverage
类型,以便从程序的每个部分进行访问。然后,在每次计算迭代之后更新这些变量。因此,您必须在代码中进行以下更改:
public static double
然后,在 public class Example {
public static double highestValue = Double.MIN_VALUE;
public static double lowestValue = Double.MAX_VALUE;
public static double highestAverage = Double.MIN_VALUE;
函数中添加以下更改:
highestChecker
最后,通过消除如下几行代码来最终修改double highestAverageChecked = calculateAverage(array);
highestAverage = Math.max(highestAverage, highestAverageChecked);
// Check lowest value
lowestValue = Math.min(lowestValue, lowestChecked);
// Check highest value
highestValue = Math.max(highestValue, highestChecked);
函数:
displayHighest
编辑:还为public static void displayHighest(double[] array) {
System.out.println("RESULTS FOR HIGHEST VALUE"
+ "\n-------------------------" + "\nHighest Average Value - "
+ highestAverage + "\nHighest Highest Value - " + highestValue
+ "\nHighest Lowest Value - " + lowestValue);
}
添加了计算。