简体   繁体   中英

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). 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.

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. Otherwise, as soon as one return statement is executed, the method is exited.

You can use lowest[0] for the day and lowest[1] for the temp, or vice versa.

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.

After you do that, you can use System.out to display the values, as desired.

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

  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.

Below is sample of return type 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):

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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