Click here to Skip to main content
15,068,879 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?



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();
}
}
Posted
Updated 1-Jun-21 4:39am
v3
Comments
Richard MacCutchan 1-Jun-21 11:25am
   
You need to reset sumTempWeek to zero after printing the total.
Ghrina 1-Jun-21 11:45am
   
How do I do this?
Richard MacCutchan 1-Jun-21 11:54am
   
Seriously?
sumTempWeek = 0;
Ghrina 1-Jun-21 12:01pm
   
Sorry my bad. I tried this outside the loop instead of right after the println and got an error message and got very confused. but it's working now. sorry again and thanks for the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900