简体   繁体   English

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

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

I am working on a program that takes user input for an array. 我正在研究一个需要用户输入数组的程序。 One method I have FindLowestTempInArray returns the lowestDay (the position in the array). 我有一个方法FindLowestTempInArray返回最低的Day(数组中的位置)。 I want to print the index and the value at that spot in my main method. 我想在我的主要方法中在该位置打印索引和值。 I have been searching and I dont know a simple way to do this. 我一直在搜索,但我不知道执行此操作的简单方法。 Right now I have been just been printing the data from the methods without returning the values. 现在,我只是从方法中打印数据而没有返回值。 That works but I want to know how to print the values from the main. 那行得通,但我想知道如何从主菜单打印值。 So once again all I am wondering is how to print both the lowestTemp and the lowestDay from the method in the main. 因此,我再次想知道如何从主方法中同时打印lowestTemp和lowestDay。

Here is my code I have: 这是我的代码:

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

One way would be to change the return type of public static int FindLowestTempInArray(int[] T) to int[] , so that you can return both values. 一种方法是将public static int FindLowestTempInArray(int[] T)的返回类型更改为int[] ,以便您可以返回两个值。 Otherwise, as soon as one return statement is executed, the method is exited. 否则,一旦执行一个return语句,该方法就会退出。

You can use lowest[0] for the day and lowest[1] for the temp, or vice versa. 您可以为当天使用lowest [0],为临时使用最低[1],反之亦然。

Another way is to get these two values separately (using parameters to distinguish which return value you want) and store them in two variables. 另一种方法是分别获取这两个值(使用参数来区分所需的返回值)并将它们存储在两个变量中。 The idea is to get these values stored somewhere in the main method to be able to play with them. 想法是将这些值存储在main方法中的某处以便能够使用它们。

After you do that, you can use System.out to display the values, as desired. 之后,您可以根据需要使用System.out显示这些值。

There are 2 ways: 有两种方法:

  1. Change the return type of your FindLowestTempInArray to int[] ie integer array and say int[0] islowest temperature and int[1] is lowest day 将您的FindLowestTempInArray的返回类型更改为int []即整数数组,并说int [0]是最低温度,而int [1]是最低温度

  2. you can create a new class say Temperature with 2 class variables say temperature and day, and in your method FindLowestTempInArray you can have return type of Temperature and you can set the Temperature object in that method. 您可以使用2个类变量(例如温度和天)创建一个新的类温度(温度),在您的方法FindLowestTempInArray中,您可以使用温度的返回类型,并可以在该方法中设置温度对象。

Below is sample of return type int[]. 下面是返回类型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;
}

} }

Solution 2(Inner Class): 解决方案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