我正在开发一个为数组提供用户输入的程序。我有FindLowestTempInArray的一种方法返回最低日(数组中的位置)。我想在main方法中打印索引和该位置的值。我一直在寻找,我不知道一个简单的方法来做到这一点。现在我刚刚从方法中打印数据而没有返回值。这有效,但我想知道如何从主要打印值。所以我想知道的是如何从main中的方法打印最低温度和最低温度。
这是我的代码:
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);
}
答案 0 :(得分:1)
一种方法是将public static int FindLowestTempInArray(int[] T)
的返回类型更改为int[]
,以便您可以返回这两个值。否则,只要执行一个return语句,就会退出该方法。
你可以使用当天的最低[0]和最低的[1],反之亦然。
另一种方法是分别获取这两个值(使用参数来区分您想要的返回值)并将它们存储在两个变量中。我们的想法是将这些值存储在main方法的某个位置,以便能够使用它们。
执行此操作后,您可以根据需要使用System.out
显示值。
答案 1 :(得分:1)
有两种方式:
将FindLowestTempInArray的返回类型更改为int [],即整数数组,并说int [0]为最低温度,int [1]为最低日期
你可以创建一个新类,说温度有2个类变量说温度和日,在你的方法FindLowestTempInArray你可以有返回类型的温度,你可以在该方法中设置温度对象。
以下是返回类型int []的示例。
public class Weather {
private static final int Uninitialized = -999;
public static void main(String[] args) {
int[] low = new int[args.length];
for (int i = 0; i < args.length; i++) {
System.out.print(args[i] + " ");
low[i] = Integer.parseInt(args[i]);
}
System.out.println("\n");
int[] lowest = new int[2];
lowest = FindLowestTempInArray(low);
System.out.println(lowest[0] + " " + lowest[1]);
}
public static int[] FindLowestTempInArray(int[] T) {
int[] lowest = new int[2];
lowest[0] = Uninitialized;
lowest[1] = 0;
for (int day = 0; day < T.length; day++) {
if (T[day] != Uninitialized
&& (T[day] < lowest[0] || lowest[0] == Uninitialized)) {
lowest[0] = T[day];
lowest[1] = day;
}
}
return lowest;
}
}
解决方案2(内部类):
public class Weather {
private static final int Uninitialized = -999;
public static void main(String[] args) {
int[] low = new int[args.length];
for (int i = 0; i < args.length; i++) {
System.out.print(args[i] + " ");
low[i] = Integer.parseInt(args[i]);
}
System.out.println("\n");
Weather.Temperature temp = FindLowestTempInArray(low);
System.out.println(temp.temperature + " " + temp.day);
}
public static Weather.Temperature FindLowestTempInArray(int[] T) {
Weather.Temperature temp=new Weather.Temperature();
temp.temperature = Uninitialized;
temp.day = 0;
for (int day = 0; day < T.length; day++) {
if (T[day] != Uninitialized
&& (T[day] < temp.temperature || temp.temperature == Uninitialized)) {
temp.temperature = T[day];
temp.day = day;
}
}
return temp;
}
static class Temperature{
private int temperature;
private int day;
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
}