• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Why does my code return zeros when calculating average values?

Ghiralt

New Coder
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("how 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[] avgT = 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();

            }


            double avgTempWeek = 0;

            {

                System.out.println("Weekly avg temp: ");

                for (int week = 1; week <= amountWeeks; week++)

                {

                    for (int inp = 1; inp <= amountTemp; inp++)

                        avgTempWeek += t[week][inp];


                    System.out.println(avgTempWeek + " ");

                    System.out.println();

                }

            }

        }

    }

}

What I have tried:

I have tried to combine sumTempWeek with avgTempWeek and also changed avgTempWeek to 1 but this does not make any difference. how do i calculate the average weekly temperature?

Java:
for (int inp = 1; inp <= amountTemp; inp++)

                sumTempWeek += t[week][inp];


                System.out.println(sumTempWeek + " ");

                System.out.println();

            }



double avgTempWeek = 0;

            {

                System.out.println("Weekly avg temp: ");

                for (int week = 1; week <= amountWeeks; week++)

                {

                    for (int inp = 1; inp <= amountTemp; inp++)

                        avgTempWeek += t[week][inp];


                    System.out.println(avgTempWeek + " ");

                    System.out.println();

                }

            }
 
Last edited by a moderator:

Top