因此,我为我的班分配了一个程序,该程序可以从文件中读取数字(int)数据,对数据进行计算,然后以格式打印结果。但是,执行的计算取决于温度。
首先,它将读取华氏温度,然后以摄氏度为单位计算温度。
如果温度为50度或更低,它将读取风速并计算华氏和摄氏温度的风寒系数,并以米/秒为单位打印出来。
如果温度为80或更高,它将读取相对湿度,计算华氏温度和摄氏温度的热量指数,然后将它们打印出来。
如果温度在50到80之间,则只需打印出华氏温度和摄氏温度。
我的问题是程序没有从文件中读取负数。
这是我的程序,(注意:我在编程时注意到了这一点,因此为什么为什么要有一个空的else-if语句和一个空的else-语句,并且将速度变量从代码中删除了而我尝试调试一些可能是由此问题引起的计算错误):
import java.io.*;
import java.util.*;
public class Program2{
public static void main (String [] args) {
//Creating file input,scanner,and exception catch
FileInputStream inf = null;
Scanner in = null;
try {
inf = new FileInputStream("weatherData.txt");
}
catch (FileNotFoundException e) {
System.out.println("You've rolled a Nat 1! Critical Failure! The file you're looking for could not be found! To top it off,this program will"
+ " now close!");
system.exit(1);
}
in = new Scanner(inf);
//Creating variables
String a; //Testing string
int tempF,//Temperature Fahrenheit
windSpeedMPH,//Wind Speed in Miles per hour
relHum; //Relative Humidity
double tempC,//Temperature Celsius
heatIndex,//Heat Index
windSpeedMPS,//Wind Speed in Meters per Second
windChillF,//Wind Chill factor in Fahrenheit
windChillC,//Wind Chill factor in Celsius
tempo1,//tempo1-13 are temporary variables to hold values
tempo2,//in order to simplify calculations
tempo3,tempo4,tempo5,tempo6,tempo7,tempo8,tempo9,tempo10,tempo11,tempo12,tempo13;
//Beginning calculation and output
while(in.hasnext()) {
//Testing while loop
//a = in.nextLine();
//System.out.println(a);
//Assigning Fahrenheit Temperature
tempF = in.nextInt();
//Calculating tempC
tempC = tempF - 32;
tempC = tempC * 0.556;
//System.out.println(tempC);
//Selection structures for calculations and outputs
if (tempF <= 50) {
windSpeedMPH = in.nextInt();
//System.out.println(windSpeedMPH);
windChillF = (35.74 + (0.6215 * tempF) - (35.75 * (Math.pow(windSpeedMPH,0.16))) + ((0.4275 * tempF) * Math.pow(windSpeedMPH,0.16)));
//System.out.println(windChillF);
System.out.println(); //Gives whitespace to separate blocks of data and make the first block visible.
System.out.printf("%-18s%-21s%10s\n","Fahrenheit Temp","Wind Speed (mph)","Feels Like");
System.out.printf("%15d%19d%15.1f\n",tempF,windSpeedMPH,windChillF);
windSpeedMPS = windSpeedMPH / 2.237;
windChillC = (35.74 + (0.6215 * tempC) - (35.75 * (Math.pow(windSpeedMPS,0.16))) + ((0.4275 * tempC) * Math.pow(windSpeedMPS,0.16)));
//System.out.println(windChillC);
System.out.printf("%15s%19s%15s\n","Celsius Temp","Wind Speed (mps)","Feels Like");
System.out.printf("%15.1f%19.1f%15.1f\n",tempC,windSpeedMPS,windChillC);
}
else if (tempF >= 80) {
}
else {
}
}
}
}
这是我的输入文件,第一列是华氏温度,第二列是风速(单位:英里/小时)或相对湿度(编辑:唯一的方法是知道第二个数字 。
>“列”是相对于相对湿度的风速,用于查看温度是50或以下(风速)还是80或以上(相对湿度)。52
-4 16
42 12
21 22
48 20
-27 18
68
62
33 0
11 2
76
0 9
12 20
79
31 31
4 8
103 98
31 17
-7 33
-16 17
36 60
-6 22
91 25
-22 5
93 88
97 12
-10 41
40 51
86 87
87 95
4 43
70
45 10
31 12
-11 33
55
104 81
-24 9
38 33
41 30
60
75
-21 11
30 2
38 23
101 88
55
21 59
83 16
15 45
-16 31
28 13
93 74
45 37
31 50
64
100 54
2 2
96 12
42 15
71
39 32
-23 15
29 9
-22 56
42 7
62
22 26
49 16
45 45
42 39
46 59
105 14
-11 28
-20 31
102 66
51
-29 14
40 46
48 26
-9 52