[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# programing output average of each month

Posted on 2011-04-24
Medium Priority
369 Views
Im trying to print the average for each month, but so far I can only print January average. any suggestion?

class tempertures365

{

public static void main(String args[])

{

int year[]=new int [365];

int i=0;

int monthSum=0;

int monthAverage=0;

int jan=0;

int feb=0;

System.out.println(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep Oct  Nov  Dec");

for (i=1;i<=31;i++)

{

year[jan]=(int)Math.floor(25+Math.random()*(38-25+1));

{

for (i=1;i<=31;i++)
{

year[feb]=(int)Math.floor(26+Math.random()*(80-26+1));

System.out.println(" "+year[jan]);

monthSum = monthSum + year[jan];

}

monthAverage = (int) (monthSum / 31);
System.out.println("Avg for Jan. is "+monthAverage);
System.out.println("Avg for Feb. is "+monthAverage);

}

}

}
}
today.JPG
0
Question by:waltbaby315
• 4

LVL 47

Expert Comment

ID: 35456979

Peerhaps you wanted something like below:

class tempertures365

{

public static void main(String args[])

{

int year[]=new int [365];

int i=0;

int monthSum=0;

int monthAverage=0;

int jan=0;

int feb=0;

System.out.println(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep Oct  Nov  Dec");

double tt = 0;
for (i=1;i<=31;i++)

{

tt +=Math.floor(25+Math.random()*(38-25+1));

}

System.out.print("" + (int) (tt/31));
tt = 0.0;
for (i=1;i<=28;i++)
{

tt +=(int)Math.floor(26+Math.random()*(80-26+1));

// System.out.println(" "+year[jan]);

//      monthSum = monthSum + year[jan];

}

System.out.print("" + (int) (tt/28));
//    monthAverage = (int) (monthSum / 31);
// System.out.println("Avg for Jan. is "+monthAverage);
// System.out.println("Avg for Feb. is "+monthAverage);

System.out.println("");
}

}

}
}
0

LVL 47

Expert Comment

ID: 35456982

Peerhaps you wanted something like below (correction):

class tempertures365

{

public static void main(String args[])

{

int year[]=new int [365];

int i=0;

int monthSum=0;

int monthAverage=0;

int jan=0;

int feb=0;

System.out.println(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep Oct  Nov  Dec");

double tt = 0;
for (i=1;i<=31;i++)

{

tt +=Math.floor(25+Math.random()*(38-25+1));

}

System.out.print("" + (int) (tt/31));
tt = 0.0;
for (i=1;i<=28;i++)
{

tt +=Math.floor(26+Math.random()*(80-26+1));

// System.out.println(" "+year[jan]);

//      monthSum = monthSum + year[jan];

}

System.out.print("" + (int) (tt/28));
//    monthAverage = (int) (monthSum / 31);
// System.out.println("Avg for Jan. is "+monthAverage);
// System.out.println("Avg for Feb. is "+monthAverage);

System.out.println("");
}

}

}
}
0

Author Comment

ID: 35456996
No it still just prints out one month
0

LVL 47

Expert Comment

ID: 35457009

it prints two moyths - there was no space between moths before

``````     int year[]=new int [365];

int i=0;

int monthSum=0;

int monthAverage=0;

int jan=0;

int feb=0;

System.out.println(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep Oct  Nov  Dec");

double tt = 0;
for (i=1;i<=31;i++)

{

tt +=Math.floor(25+Math.random()*(38-25+1));

}

System.out.print("" + (int) (tt/31) + "    ");
tt = 0.0;
for (i=1;i<=28;i++)
{

tt +=Math.floor(26+Math.random()*(80-26+1));

// System.out.println(" "+year[jan]);

//      monthSum = monthSum + year[jan];

}

System.out.print("" + (int) (tt/28)  + "      ");
//    monthAverage = (int) (monthSum / 31);
// System.out.println("Avg for Jan. is "+monthAverage);
// System.out.println("Avg for Feb. is "+monthAverage);

System.out.println("");

``````
0

LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 35457014

This is full code with class, main, etc

``````class tempertures365

{

public static void main(String args[])

{

int year[]=new int [365];

int i=0;

int monthSum=0;

int monthAverage=0;

int jan=0;

int feb=0;

System.out.println(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep Oct  Nov  Dec");

double tt = 0;
for (i=1;i<=31;i++)

{

tt +=Math.floor(25+Math.random()*(38-25+1));

}

System.out.print("" + (int) (tt/31)  + "      ");
tt = 0.0;
for (i=1;i<=28;i++)
{

tt +=Math.floor(26+Math.random()*(80-26+1));

// System.out.println(" "+year[jan]);

//      monthSum = monthSum + year[jan];

}

System.out.print("" + (int) (tt/28) + "       ");
//    monthAverage = (int) (monthSum / 31);
// System.out.println("Avg for Jan. is "+monthAverage);
// System.out.println("Avg for Feb. is "+monthAverage);

System.out.println("");
}

}

}
}
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
###### Suggested Courses
Course of the Month19 days, left to enroll