所以我需要程序打印出每周平均温度的平均值,但我无法找到解决方法。目前我的列打印出一周的平均温度为deeplyNested = True
thing = 'feedback'
class Contrived(object):
def __init__(self):
if deeplyNested:
logger.info(
("Sometimes there's a lot to say in %s and you need to write"
" longer messages than what nicely fits in the 80 column"
" limit."),
thing
)
,因此它不在二维数组中,但现在我需要找到这些平均值的平均值。我尝试使用System.out.print
来查找平均值,但打印出的数字不正确。我该怎么写这个代码?
System.out.print(average/4);
答案 0 :(得分:1)
确定您的代码中的以下更改
average[] = new double[Temperature.length];
将此average = columnTotal / 7
; System.out.println(average);
替换为此
average[i] = columnTotal / 7 ;
System.out.println(average[i]);
所以我正在做什么存储数组中的平均值。你将平均值存储在一个简单的变量中,这个变量一次又一次地被覆盖直到循环结束
最后用
替换代码的最后一部分columnTotal = 0;
for (int i = 0; i < average.length; i++) {
columnTotal = columnTotal +average[i];
}
System.out.print((columnTotal / 4) + " ");
最后你的代码看起来像这样
import java.text.DecimalFormat;
import java.util.Scanner;
public class TwoDimArray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 1) Declare and allocate Storage
//Each day name is assigned to the array as a String so each name must be enclosed in apostrophes and each string must be separated by commas.
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
double[][] Temperature = new double[4][7];
// 2) Populate Array
for (int i = 0; i < Temperature.length; i++) {
for (int j = 0; j < Temperature[0].length; j++) {
//days[j] will print the proper day and "(i+1)" will print the proper day of the week
System.out.print("Enter " + days[j] + " Temperature " + "for Week " + (i + 1) + ": " + " ");
Temperature[i][j] = scan.nextDouble();
}
}
System.out.println();
System.out.println(" Weekly Temperature Report ");
System.out.println("Mon Tue Wed Thurs Fri Sat Sun Average");
double columnTotal, average[] = new double[Temperature.length];
// 4) Output Array
for (int i = 0; i < Temperature.length; i++) {
System.out.println();
columnTotal = 0;
for (int j = 0; j < Temperature[0].length; j++) {
System.out.print(Temperature[i][j] + "\t");
columnTotal += Temperature[i][j];
}
average[i] = columnTotal / 7;
System.out.println(average[i]);
}
System.out.println();
System.out.println("------------------------------------------------------------------");
columnTotal = 0;
for (int i = 0; i < average.length; i++) {
columnTotal = columnTotal +average[i];
}
System.out.print((columnTotal / 4) + " ");
//System.out.print(average/4);
}
}