【发布时间】:2016-09-24 04:14:32
【问题描述】:
在这个程序中,我从 .txt 文件(以下文件)中检索高温和低温列表987654322@ 方法,所以我可以从一维获得最高和最低温度,但是当我编译并运行它时,我得到了错误的输入,我已经通过调试器多次运行了两个 for 循环,并且找不到原因,可以谁能告诉我为什么我的两种方法都不起作用?
text.txt 文件
JAN 48 2
FEB 53 6
MAR 57 30
APR 54 34
MAY 73 37
JUN 85 52
JUL 91 59
AUG 87 61
SEP 81 49
OCT 63 38
NOV 61 29
DEC 52 17
温度等级
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Temperature
{
static String fileName, line;
static int high;
public static void main(String[] args) throws IOException
{
int[][] temps = new int[12][2];
Scanner scanner = new Scanner(new File("text.txt"));
for(int i=0;i<temps.length;i++)
{
String months = scanner.next();
int high = scanner.nextInt();
temps[i][0] = high;
int low = scanner.nextInt();
temps[i][1] = low;
}
System.out.println(Arrays.deepToString(temps)); //printed the whole array to make sure placed correctly
int highestTemp = getHighTemp(temps);
System.out.println("Highest Temp: " + highestTemp);
int lowestTemp = getLowTemp(temps);
System.out.println("Lowest Temp: " + lowestTemp);
}
public static int getHighTemp(int[][] arr)
{
int high = arr[0][0];
for(int i = 0; i<11;i++){
int nextValue = arr[i+1][0];
if(high < nextValue)
high = nextValue;
else
high = arr[i][0];
}
return high;
}
public static int getLowTemp(int[][] arr)
{
int low = arr[0][0];
for(int i = 0; i<11;i++)
{
int nextValue = arr[i+1][0];
if(low > nextValue)
low = nextValue;
else
low = arr[i][0];
}
int lowestValue = low;
return lowestValue;
}
}
【问题讨论】:
-
它没有任何错误。我有以下输出: [[48, 2], [53, 6], [57, 30], [54, 34], [73, 37], [85, 52], [91, 59] , [87, 61], [81, 49], [63, 38], [61, 29], [52, 17]] 最高温度:61 最低温度:52
-
@DanishAmjad 最高温度应该是 91,因为它只应该通过 [0-11][0] 的值,61 在 [0-11][1]
-
我的意思是我想要 [0-11][0] 中的最高输入,我不想检查第三列的任何内容,我只想要第二行 @DanishAmjad
标签: java for-loop logic logical-operators