我想使用for循环而不是每个循环打印出给定数字列表的最小数量和平均数?我试过但它不起作用可能是我在某处做错误或者我没有理解这个概念......?任何帮助将不胜感激......
public class MaxMin {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
System.out.println("Max is " + max);
}
}
答案 0 :(得分:0)
找到最低要求:
// Finding the min element
double min = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] < min) {
min = myList[i];
}
}
要找到平均值,你就已经得到了总和:
// find average:
double avg = total/myList.length;