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.
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?
– Android Programmer
@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 thej
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();
– ramaral
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?
– Android Programmer
@Beginnerteemjava What is your question? It is giving error?
– ramaral
Yes, it keeps going wrong. You can test your code there, and then put it here when you are running?
– Android Programmer
@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.– ramaral
@Beginnerteemjava CORRECTION: At the beginning put the following line:
double[][] temperaturas = new double[12][31];
– ramaral