Doubt about how to fill two-dimensional array in Java

Asked

Viewed 981 times

1

I’m doing an exercise and I need help. That’s the statement:

Calculate average temperatures for each month. For 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 of each month.

Here follows the code I’ve already made, but I don’t 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++);

To not get too big this test, you can do it with smaller numbers, for example, 3 months, and each month contains 10 days (then you have to write 30 values (3 * 10), to get the result of the months). 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 doubts:

  • And how you get to write the name of every month on the line?

  • How to fill in the values in each column?

Please, can someone help me make this algorithm?

Ah, there’s one more restriction: I can’t use the get, the set, the throw and the this, because what I am doing is very basic and of level well beginner.

1 answer

1

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 start at 1, but the array starts at 0.

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

I hope it helps you and you can develop your algorithm.

  • Okay, thank you very much. One question, please: In the sentence: "for (int j = 1; j <= diasNoMes[i]; j++) {" It shouldn’t be: for (int j = 0; j<= diasnoMes[i]; j++) { Since j will start at the first position that is zero?

  • @Beginner Java The reason of j start at 1 is for that text "Digite a temperatura do dia " + j ..... don’t stay "Digite a temperatura do dia 0"... Note that later, when the j is used to index the array to 1: temperaturas[i][j - 1] = sc.nextDouble();. Maybe it’s more intuitive to start at zero and add 1 to the text: for (int j = 0;.... System.out.print("Digite a temperatura do dia " + (j + 1) .... temperaturas[i][j] = sc.nextDouble();

  • How would the method look: public Static double[] calcularArrays (double[] mes) { int soma = 0; int media = 0; String meses [] = {"Janeiro", "Fevereiro", "Março"}; int diasNoMes = {5,5,5}; for (int i = 0; i<months.length;i++) { String nameMes = months[i]; for (int j = 1; j<=daysNoMes[i]; j++) { System.out.println("Enter the day temperature "+j+" of " +nameMes + ":"); temperatures[i][j-1] = sc.nextDouble(); sum += temperatures [j]; media = sum/daysNoMes.length; Return media[i]; } } } } Are you all right? How would I look to fix and execute the code perfectly?

  • @Beginnerteemjava What is your question? It is giving error?

  • Yes, it keeps going wrong. You can test your code there, and then put it here when you are running?

  • @Beginner Java Understand that the stackoverflow does not work like this. Be specific in your doubt. If it gives error say which. I think it should be because of the array temperaturas not have been declared. At the beginning put the following line: double temperaturas[12][31];. Note that the code posted in the reply only serves to ask the user to indicate the temperatures on each day of the month.

  • @Beginnerteemjava CORRECTION: At the beginning put the following line: double[][] temperaturas = new double[12][31];

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.