[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
5
Medium Priority
?
369 Views
Last Modified: 2013-11-23
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
Comment
Question by:waltbaby315
  • 4
5 Comments
 
LVL 47

Expert Comment

by:for_yan
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

by:for_yan
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

by:waltbaby315
ID: 35456996
No it still just prints out one month
0
 
LVL 47

Expert Comment

by:for_yan
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("");
            

Open in new window

0
 
LVL 47

Accepted Solution

by:
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("");
         }  

  } 


}
} 

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question