繁体   English   中英

通过方法打印数组的值和位置

[英]Printing the value and position of an array from a method

我正在研究一个需要用户输入数组的程序。 我有一个方法FindLowestTempInArray返回最低的Day(数组中的位置)。 我想在我的主要方法中在该位置打印索引和值。 我一直在搜索,但我不知道执行此操作的简单方法。 现在,我只是从方法中打印数据而没有返回值。 那行得通,但我想知道如何从主菜单打印值。 因此,我再次想知道如何从主方法中同时打印lowestTemp和lowestDay。

这是我的代码:

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);
}

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

您可以为当天使用lowest [0],为临时使用最低[1],反之亦然。

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

之后,您可以根据需要使用System.out显示这些值。

有两种方法:

  1. 将您的FindLowestTempInArray的返回类型更改为int []即整数数组,并说int [0]是最低温度,而int [1]是最低温度

  2. 您可以使用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;
    }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM