提问人:Blut_Regnet 提问时间:3/26/2020 最后编辑:Blut_Regnet 更新时间:3/26/2020 访问量:64
扫描负值并将其从文件分配给 int
Scanning and assigning a negative value from a file to an int
问:
因此,我为我的班级布置了一个任务,即编写一个程序,该程序从文件中读取数字 (int) 数据,对数据执行计算,然后以格式打印结果。但是,执行的计算取决于它的热度。
首先,它将读取华氏温度,然后以摄氏度计算温度。
如果温度为 50 度或更低,它将读取风速并计算以华氏度和摄氏度为单位的风寒系数,将它们打印出来,以及以米/秒为单位的风速。
如果温度为 80 度或更高,它将读取相对湿度,计算华氏度和摄氏度的热指数,然后打印出来。
如果温度在 50 到 80 之间,那么它只会打印出华氏度和摄氏度的温度。
我的问题是程序没有从文件中读取负数。
这是我的程序,(注意:我在编程过程中注意到了这一点,因此为什么有一个空的 else-if 语句和一个空的 else-statement,并且在我尝试调试一些可能由此问题引起的计算错误时从代码中删除了 tempo 变量):
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
答: 暂无答案
评论
wind speed in miles per hour
relative humidity