/ / जावा में तापमान संगणना? - जावा, लूप, तापमान

जावा में तापमान गणना? - जावा, लूप, तापमान

मूल रूप से, कार्य तापमान की गणना करना हैएक फ्रीजर में डालने के बाद एक गर्म वस्तु। फ्रीजर का तापमान -20 डिग्री पर स्थिर है। एक बार जब ऑब्जेक्ट फ्रीज़र में होता है, तो इसका तापमान प्रत्येक सेकंड में (K * dt) डिग्री तक गिर जाता है, जहाँ K = 0.001 और dt, वर्तमान ऑब्जेक्ट तापमान और फ्रीज़र के तापमान के बीच का अंतर होता है। यह विधि कुछ सेकंड के लिए फ्रीज़र में रहने के बाद ऑब्जेक्ट के तापमान (शुरुआती तापमान के साथ) की गणना करने में सक्षम होना चाहिए।

समाधान में एक लूप की सुविधा होनी चाहिए जो गणना करता हैवस्तु के बदलते तापमान का दूसरा-दूसरा। प्रत्येक सेकंड में, तापमान (K * dt) से गिरना चाहिए जहाँ dt वर्तमान वस्तु तापमान (जैसा कि उस दूसरे के शुरू में है) और फ्रीज़र तापमान के बीच अंतर है यहाँ मेरा कोड है:

import java.util.Scanner;

public class Cooling {

public static final double FREEZER_TEMPERATURE = -20;
public static final double K = 0.001;

public static void main(String[] args) {
temperatureTest(70, 0);
temperatureTest(70, 60);
}

public static double temperature(double initialTemperature, int seconds) {
double x = initialTemperature;
int y = seconds;
double dt = (x - FREEZER_TEMPERATURE);
return K*dt*y ;
}

मेरी समस्या यह है कि मैं यह नहीं जानता कि इसे लूप में कैसे लागू किया जाए। किसी भी मदद की सराहना की जाएगी :) धन्यवाद। :)।

उत्तर:

उत्तर № 1 के लिए 1

यह विधि वह करेगी। मैंने प्रिंट स्टेटमेंट जोड़ा है ताकि आप प्रगति देख सकें, लेकिन आप इसे हटाना चाहते हैं।

public static double temperature(double initialTemperature, int seconds) {
double currentTemp = initialTemperature;
for (int time = 1; time <= seconds; time++) {
currentTemp -= K * (currentTemp - FREEZER_TEMPERATURE);
System.out.printf("After %d seconds, temperature is %f%n", time, currentTemp);
}
return currentTemp;
}

परीक्षा

System.out.println(temperature(70, 60));

उत्पादन

After 1 seconds, temperature is 69.910000
After 2 seconds, temperature is 69.820090
After 3 seconds, temperature is 69.730270
After 4 seconds, temperature is 69.640540
After 5 seconds, temperature is 69.550899
After 6 seconds, temperature is 69.461348
After 7 seconds, temperature is 69.371887
After 8 seconds, temperature is 69.282515
After 9 seconds, temperature is 69.193232
After 10 seconds, temperature is 69.104039
After 11 seconds, temperature is 69.014935
After 12 seconds, temperature is 68.925920
After 13 seconds, temperature is 68.836994
After 14 seconds, temperature is 68.748157
After 15 seconds, temperature is 68.659409
After 16 seconds, temperature is 68.570750
After 17 seconds, temperature is 68.482179
After 18 seconds, temperature is 68.393697
After 19 seconds, temperature is 68.305303
After 20 seconds, temperature is 68.216998
After 21 seconds, temperature is 68.128781
After 22 seconds, temperature is 68.040652
After 23 seconds, temperature is 67.952611
After 24 seconds, temperature is 67.864659
After 25 seconds, temperature is 67.776794
After 26 seconds, temperature is 67.689017
After 27 seconds, temperature is 67.601328
After 28 seconds, temperature is 67.513727
After 29 seconds, temperature is 67.426213
After 30 seconds, temperature is 67.338787
After 31 seconds, temperature is 67.251448
After 32 seconds, temperature is 67.164197
After 33 seconds, temperature is 67.077033
After 34 seconds, temperature is 66.989956
After 35 seconds, temperature is 66.902966
After 36 seconds, temperature is 66.816063
After 37 seconds, temperature is 66.729247
After 38 seconds, temperature is 66.642517
After 39 seconds, temperature is 66.555875
After 40 seconds, temperature is 66.469319
After 41 seconds, temperature is 66.382850
After 42 seconds, temperature is 66.296467
After 43 seconds, temperature is 66.210170
After 44 seconds, temperature is 66.123960
After 45 seconds, temperature is 66.037836
After 46 seconds, temperature is 65.951798
After 47 seconds, temperature is 65.865847
After 48 seconds, temperature is 65.779981
After 49 seconds, temperature is 65.694201
After 50 seconds, temperature is 65.608507
After 51 seconds, temperature is 65.522898
After 52 seconds, temperature is 65.437375
After 53 seconds, temperature is 65.351938
After 54 seconds, temperature is 65.266586
After 55 seconds, temperature is 65.181319
After 56 seconds, temperature is 65.096138
After 57 seconds, temperature is 65.011042
After 58 seconds, temperature is 64.926031
After 59 seconds, temperature is 64.841105
After 60 seconds, temperature is 64.756264
64.75626360008506

उत्तर № 2 के लिए 1
for(int i =0; i<seconds; i++)
{
currentTemp -= currentTemp*K*(currentTemp - FREEZER_TEMPERATURE);
}

जवाब के लिए 0 № 3
public static void main(String[] args) {

for(int t = 0, t < NUMBER OF SECONDS YOU WANT; t++) {
temperatureTest(YOUR INITIAL TEMP, t);
}
}