Results 1 to 7 of 7
- 05-01-2011, 04:48 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
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
- 05-01-2011, 06:03 PM #2
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Where are you incrementing the month? Seems to stay at month=0 the entire time.
- 05-01-2011, 07:14 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
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
- 05-01-2011, 07:14 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
I'm wondering would I need another for loop
- 05-01-2011, 07:15 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
or output month 5
- 05-01-2011, 07:33 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Nope, a single loop is enough; make 'month' the index of the month with the highest temperature; something like this:
kind regards,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
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-01-2011, 10:42 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Temperature analysis program
By Evii0 in forum New To JavaReplies: 8Last Post: 03-29-2011, 09:05 AM -
How to write a program to calculate and display sum in two dimensional array?
By Javanoobs in forum New To JavaReplies: 12Last Post: 02-08-2011, 02:58 PM -
How to write a program to calculate and display sum in two dimensional array
By Javanoobs in forum Advanced JavaReplies: 1Last Post: 02-08-2011, 09:11 AM -
temperature program with inputs
By JingGong in forum New To JavaReplies: 3Last Post: 10-17-2008, 06:34 PM -
Switch statement to display the name of the month
By Java Tip in forum Java TipReplies: 0Last Post: 01-04-2008, 09:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks