Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • 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:
I think you are adding { and } where they do not need to be. Try with only those for functions, for statements, and if and else if and else statements. I think you should be getting an error with this and your calculate average temp and sum temp thing is like in a separate function so it is not run. Also, if you want all temps, start at 0, not 1, X E.
 

Buy us a coffee!

Back
Top Bottom