Java:
import java.util.*; // Scanner, Locale
class TempTest {
public static void main(String[] args) {
System.out.println("TEMPERATURES\n");
Scanner in = new Scanner(System.in);
in.useLocale(Locale.US);
System.out.print("hoe many weeks: ");
int amountWeeks = in.nextInt();
System.out.print("how many temperatures per week: ");
int amountTemp = in.nextInt();
double[][] t = new double[amountWeeks + 1][amountTemp + 1];
for (int week = 1; week <= amountWeeks; week++) {
System.out.println("temperatures - weekly " + week + ":");
for (int inp = 1; inp <= amountTemp; inp++)
t[week][inp] = in.nextDouble();
}
System.out.println();
System.out.println("temperatures:");
for (int week = 1; week <= amountWeeks; week++) {
for (int inp = 1; inp <= amountTemp; inp++)
System.out.print(t[week][inp] + " ");
System.out.println();
}
System.out.println();
double[] sumT = new double[amountWeeks + 1];
double sumTempWeek = 0;
{
System.out.println("Weekly temp sum: ");
for (int week = 1; week <= amountWeeks; week++)
{
for (int inp = 1; inp <= amountTemp; inp++)
sumTempWeek += t[week][inp];
System.out.println(sumTempWeek + " ");
System.out.println();
}
}
}
}
What I have tried:
I have tried the following code to get the weekly sum of temperatures from the program and the first return is always correct and gives me week no.1 sum temp but week 2 gives me week no.1 and week no.2 sum temperatures combined. what is wrong with my code?
Java:
double[] sumT = new double[amountWeeks + 1];
double sumTempWeek = 0;
{
System.out.println("Weekly temp sum: ");
for (int week = 1; week <= amountWeeks; week++)
{
for (int inp = 1; inp <= amountTemp; inp++)
sumTempWeek += t[week][inp];
System.out.println(sumTempWeek + " ");
System.out.println();
}
}
Last edited by a moderator: