Doubt about filling a two-dimensional array in Java

2

I'm doing an exercise and I need help. Here is the statement:

  

Calculate the average temperatures of each month. To do this, create a list containing the names of each month in the rows, and the number of days of each month in the columns. On each day, store each temperature value written by the user. Then average the temperatures for each month.

Here is the code I've already done, but I do not even know how to do it right since I'm a beginner in Java:

import java.util.Scanner;

public class MediaTemperaturas {

public static void main (String args[]) {

double mês = new double[12][31];

for (int i = 0; i<11; i++);

Scanner sc = new Scanner (System.in);

String nomesMeses = sc.nextLine();

for (int j = 0; j<30;j++);

In order not to get too large, this test can be done with smaller numbers, for example 3 months, and each month contains 10 days (in which case you have to write 30 values (3 * 10) to reach the result of months). You can do:

double mes = new double [3][10];

**//Pra calcular a média, faz um:**

 int soma = 0;

 int media = 0;

 if (int i = 0; i < dias.length; i++) {



   soma += dias;

   media = soma/dias.length;

   return media;

}

My questions:

  • And how do you get the name of each month on the line?

  • How do you fill in the values in each column?

Can someone please help me with this Algorithm?

Ah, there's one more restriction: I can not use get , set , throw , and this , because what I'm doing is pretty basic and very beginner level. >     

asked by anonymous 10.07.2016 / 03:09

1 answer

0

Come on. First:

String[] meses = {
    "Janeiro", "Fevereiro", "Março", "Abril",  "Maio", "Junho",
    "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"
};

// Vamos dizer que fevereiro tem 29 dias porque este ano é bissexto.
int[] diasNoMes = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Second, you can go through the days as follows:

for (int i = 0; i < meses.length; i++) {
    String nomeMes = meses[i];
    for (int j = 1; j <= diasNoMes[i]; j++) {
        System.out.print("Digite a temperatura do dia " + j + " de " + nomeMes + ":");
        temperaturas[i][j - 1] = sc.nextDouble();
    }
}

That j - 1 instead of just j is because the days begin at 1, but the array starts at 0.

And if your temperatures are double , then your soma also has to be double and media also has to be double .

I hope this helps you and that you can develop your algorithm.

    
10.07.2016 / 05:14