我第一次尝试数值求解微分方程。我的程序会要求输入一些参数,然后计算达到给定温度所需的冷却时间。以下是我的主函数:
import java.util.Scanner;
public class Step2_lab11 {
public static int TAO = 50;
public static double DELTA_MINUTES = 0.1;
public static void main(String[] args) {
System.out.println("VINETS AVKYLNINGSTID \n");
System.out.println("Ange vinets temperatur:");
Scanner userIn = new Scanner(System.in);
double wineTemp = userIn.nextDouble();
System.out.println("Vinets önskade temperatur:");
double preferredTemp = userIn.nextDouble();
System.out.println("Kylens/frysens temperatur:");
double chillTemp = userIn.nextDouble();
WineChiller wineChiller = new WineChiller();
double elapsedTime = 0.0;
while(wineTemp > preferredTemp) {
elapsedTime = elapsedTime + DELTA_MINUTES;
double dT = wineChiller.getChillingTime(TAO, DELTA_MINUTES, chillTemp, preferredTemp, wineTemp);
wineTemp = wineTemp - dT;
System.out.println(elapsedTime);
}
}
}
以下是WineChiller.java文件的内容:
public class WineChiller {
public WineChiller() {
}
public double getChillingTime(int TAO, double DELTA_MINUTES, double chillTemp, double preferredTemp, double wineTemp) {
double dT = (wineTemp - chillTemp) * DELTA_MINUTES / TAO;
return dT;
}
}
在这个while循环中,Syso部分会输出以下内容(其中wineTemp = 25,preferredTemp = 16,chillTemp = 5)。
0.1
0.2
0.30000000000000004
....
29.300000000000146
29.400000000000148
29.50000000000015
29.60000000000015
29.700000000000152
29.800000000000153
29.900000000000155
不知道为什么会添加随机小数。我也认为(但我不确定)正确答案应该恰好是30分钟,而不是29.9。我是否错过了一些明显的逻辑错误?
BigDecimal
吗? - Thorn G