【问题标题】:Printing the value and position of an array from a method从方法打印数组的值和位置
【发布时间】: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


【解决方案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;
    }
}

}

【讨论】:

  • 不幸的是我只能使用一个类。我还能在一节课上做#2吗?如果我创建了像 @TomN 评论这样的实例变量?
  • 可以,创建一个静态内部类。我也用内部类的解决方案修改了我的答案。现在再次查看我上面的答案。
  • 我在想一些不同的东西,因为我以前没有学到过类似的东西。感谢您的解释!
【解决方案2】:

一种方法是将public static int FindLowestTempInArray(int[] T) 的返回类型更改为int[],这样您就可以返回这两个值。否则,只要执行了一个 return 语句,就退出该方法。

您可以使用最低[0] 表示当天,最低[1] 表示温度,反之亦然。

另一种方法是分别获取这两个值(使用参数来区分你想要哪个返回值),并将它们存储在两个变量中。我们的想法是将这些值存储在 main 方法中的某个位置,以便能够使用它们。

完成此操作后,您可以根据需要使用System.out 显示值。

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多