【发布时间】:2016-05-03 21:56:18
【问题描述】:
我正在开发一个接受用户输入数组的程序。我有 FindLowestTempInArray 的一种方法返回最低日(数组中的位置)。我想在我的主要方法中打印该位置的索引和值。我一直在寻找,我不知道一个简单的方法来做到这一点。现在我只是从方法中打印数据而不返回值。那行得通,但我想知道如何从主打印值。所以我再次想知道的是如何从主方法中打印最低温度和最低日期。
这是我的代码:
public static int FindLowestTempInArray(int[] T)
{
// Returns the index of the lowest temperature in array T
int lowestTemp = Uninitialized;
int lowestDay = 0;
for(int day = 0; day < T.length; day++)
{
if(T[day] != Uninitialized && ( T[day] < lowestTemp || lowestTemp == Uninitialized))
{
lowestTemp = T[day];
lowestDay = day;
return lowestTemp;
}
}
return lowestDay;
}
public class Weather {
private static final int Uninitialized = -999;
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] high = new int[32];
int [] low = new int[32];
Init (high);
Init(low);
LoadData(high,low);
Report(high, low);
FindAvg(high);
//FindAvg(low);
//why do i not need to do both the one above and FindAvg(low);
System.out.println("The average for the high is: " + FindAvg(high));
System.out.println("The average for the low is: " + FindAvg(low));
//Lowest(high, low);
FindLowestTempInArray(high);
System.out.println(FindLowestTempInArray(high) + "\n" + FindLowestTempInArray(low));
Highest(high,low);
System.out.println("\n" + "The highest high is: " + Highest(high, low) + " degrees." + "\n" +
"This temperature was recorded on day: " + Highest(high, low));
System.out.println("\n" + "The highest low is: " + Highest(low, high) + " degrees." + "\n" +
"This temperature was recorded on day: " + Highest(low, high));
// LeastToGreatest(high, low);
}
【问题讨论】:
-
您不能通过运行一次 int 方法返回两个值。尝试将最低天和最低温度作为实例变量。调用方法时,将值保存到变量中。
-
@TomN 所以我可以将在类中定义为公共静态 int 最低温度的实例变量。之后,我可以在 FindLowestTempInArray 中使用该变量,但如何再次调用 main 中 println 中的实例变量?
-
你如何尝试?它应该就像postimg.org/image/fhjq5veu9
-
请注意这是类变量而不是实例变量,我的错。
-
@TomN 如果删除 int[] T 那么 for 循环和 if 语句应该如何工作?
标签: java arrays methods return