java - Returning values from a second array -
currently code returns highest temperature value submitted when user enters in temperature values each month. how can program return month highest entry, instead of highest entry itself?
import java.util.scanner; public class reader { static string months[] = { "january" , "february" , "march" , "april" , "may" , "june", "july", "august", "september", "october", "november", "december" }; public static void main(string[] args){ int[] avgmonth; int temprecords = 0; double tempsum = 0; double avgtemp; double gethotmonth; //collect user input avg temp , put array month scanner input = new scanner(system.in); avgmonth = new int[months.length]; for(int i=0; < months.length; i++){ system.out.println("enter average temperature month of "+months[i]); avgmonth[i] = input.nextint(); } //call avgtemp, takes array of temps argument, return total avg year for(int i=0;i<months.length; i++) tempsum += avgmonth[i]; avgtemp = tempsum/months.length; //call gethotmonth, takes entire array argument, find index of hottest month gethotmonth = avgmonth[0]; (int i=0;i<months.length;i++){ if (avgmonth[i] > gethotmonth) gethotmonth = avgmonth[i]; } //displayresults, display average , hottest month //args average , index array number of hottest month //final output displayresults(avgtemp,gethotmonth); input.close(); }//close main public static void displayresults(double average, double gethotmonth){ system.out.println("the average temperature year "+average+" degrees f "+gethotmonth+" being hottest month."); } }
you need capture hotmonth
during iteration , send argument displayresults
method shown below:
public static void main(string[] args){ //add existing code string hotmonth=""; (int i=0;i<months.length;i++){ if (avgmonth[i] > gethotmonth) { gethotmonth = avgmonth[i]; hotmonth = months[i];//capture hotmonth } } displayresults(avgtemp,hotmonth); } public static void displayresults(double average, string hotmonth){ system.out.println("the average temperature year "+average+" degrees f "+ hotmonth+" being hottest month."); }
Comments
Post a Comment