My problem is to design a program that uses a mathematical model for heat loss, or Tnew = Told - m(Told - Tair). It also asks me to repeat the code for 60 minutes, generating a new answer every minute. I have completed the code, however I receive no output. There are no build errors. What am I doing wrong?

package hotpotato;

/**
*
* @author Josh
*/
public class Main {
public static void heatLoss (double m, double tO, double tA) {
for (int i = 1; i == 60; i = i +1) {
// int n represents 60 minutes,
double tNew = tO - m * (tO - tA);
tNew = tO;
// Defines tNew and the next minute's tO

System.out.println ("New temperature: " + tNew);
// Output New Temp

}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
heatLoss (.5, 100, 90);
}
// TODO code application logic here
}

for (int i = 1; i == 60; i = i +1) {

The loop executes as long the condition: i==60 is true. If it is false the loop exits.
So you begin with i=1. Then the condition is checked: i==60 which is false, so the loop …

All 4 Replies

for (int i = 1; i == 60; i = i +1) {

The loop executes as long the condition: i==60 is true. If it is false the loop exits.
So you begin with i=1. Then the condition is checked: i==60 which is false, so the loop exits without doing anything.
You want the loop to executes for i=1,2,3,4,....60. So you want i<=60

Ah, alright. I thought it would execute until i = 60. Thank you so much.
If I have any more questions I will be sure to post.

Revised code:
With suggestions by javaAddict and my own corrections

package hotpotato;

/**
*
* @author Josh
*/
public class Main {
public static void heatLoss (double m, double tO, double tA) {
for (int i = 1; i <= 60; i++) {
// int n represents 60 minutes,
double tN = (tO - m * (tO - tA));
// Defines tNew
System.out.println ("New temperature: " + tN);
// Output New Temp
tO = tN;

}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
heatLoss (.5, 100, 90);
}
// TODO code application logic here
}

You can also have the 60 to be the argument:

package hotpotato;

/**
*
* @author Josh
*/
public class Main {
public static void heatLoss (double m, double tO, double tA, int minutes) {
for (int i = 1; i <= minutes; i++) {
// int n represents 60 minutes,
double tN = (tO - m * (tO - tA));
// Defines tNew
System.out.println ("New temperature: " + tN);
// Output New Temp
tO = tN;

}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
heatLoss (.5, 100, 90, 60);
}
// TODO code application logic here
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.