Results 1 to 7 of 7
  1. #1
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default outputting variable to correspond with another variable

    I have everything else working in this program but I cannot work out how to display what month the top temperature falls on.I have declared month as a variable and can get the program to request a different month for each input and it is outputting the top temp and the average temp just fine but not what month the top temperature is.any help would be greatly appreciated




    package practical18;

    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;


    public class Pract18 {



    public static void main(String[] args) {
    DecimalFormat twoDigits=new DecimalFormat("0.00");

    String strTemp;

    double temp=0,topTemp=0,avg=0,total=0;

    int counter,month=0;



    double tempArray[]= new double [12];

    for (counter=0;counter<tempArray.length;counter++){
    strTemp=JOptionPane.showInputDialog("Enter temperature for month "+(counter+1));
    temp=Double.parseDouble(strTemp);
    tempArray[counter]=temp;}

    for(counter=0;counter<tempArray.length;counter++){
    if(tempArray[counter]>topTemp){
    topTemp=tempArray[counter];


    }

    for(counter=0;counter<tempArray.length;counter++){
    total=total+tempArray[counter];
    }




    }
    avg=total/tempArray.length;


    JOptionPane.showMessageDialog
    (null,"the highest temp was "+twoDigits.format(topTemp)+" in month "+month+
    " the average temperature is "+twoDigits.format(avg));






    }



    }
    Last edited by leoshiner; 05-01-2011 at 05:22 PM. Reason: title too vague

  2. #2
    Durden is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Where are you incrementing the month? Seems to stay at month=0 the entire time.

  3. #3
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default

    well I was going to increment it just after the line of code
    topTemp=tempArray[counter];

    but it still comes up with month 1 regardless

    if I input the highest temperature say on the fifth month I want it to output fifth month at the end

  4. #4
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default

    I'm wondering would I need another for loop

  5. #5
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default

    or output month 5

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by leoshiner View Post
    I'm wondering would I need another for loop
    Nope, a single loop is enough; make 'month' the index of the month with the highest temperature; something like this:

    Java Code:
    month= -1; // no valid index yet.
    for (int i= 0; i < months.length; i++) // loop over the months
       if (month == -1 || months[i] > months[month]) // higher?
          month= i; // remember highest index of temperature so far
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    leoshiner is offline Member
    Join Date
    Oct 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Nope, a single loop is enough; make 'month' the index of the month with the highest temperature; something like this:

    Java Code:
    month= -1; // no valid index yet.
    for (int i= 0; i < months.length; i++) // loop over the months
       if (month == -1 || months[i] > months[month]) // higher?
          month= i; // remember highest index of temperature so far
    kind regards,

    Jos
    thanks that worked a treat as soon as I was able to get my head around it

Similar Threads

  1. Temperature analysis program
    By Evii0 in forum New To Java
    Replies: 8
    Last Post: 03-29-2011, 09:05 AM
  2. Replies: 12
    Last Post: 02-08-2011, 02:58 PM
  3. Replies: 1
    Last Post: 02-08-2011, 09:11 AM
  4. temperature program with inputs
    By JingGong in forum New To Java
    Replies: 3
    Last Post: 10-17-2008, 06:34 PM
  5. Replies: 0
    Last Post: 01-04-2008, 09:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •