对数组进行排序并从中选择值

时间:2017-11-13 13:49:54

标签: java

在调整我的代码并转到第二部分之后,我不确定为什么第2部分的输出不正确并且它主要输出0和其他数字不在数组中?我为数组输出一个输出只是为了检查发生了什么,但你几乎可以忽略它。不知道我哪里出错任何指针?这次我还包括了我的任务:

//temperature management
import java.util.Scanner;
import java.util.Arrays;
public class temperaturemanagement
{
    Scanner in = new Scanner(System.in);
    //task 1
    //To simulate the monitoring required, write a routine that allows entry of the apartment’s temperature in degrees Celsius. The routine checks whether the temperature is within the acceptable range, too high or too low and outputs a suitable message in each case.
    public void task1()
    {
        System.out.println("Input Temperature: ");
        double temperature = in.nextDouble();
        double temperaturerounded = Math.round(temperature * 10) / 10.0;
        if (temperaturerounded >=22 && temperaturerounded <=24)
        {
            System.out.println("Normal");
        }
        else if (temperaturerounded <22)
        {
            System.out.println("Too Low");
        }
        else if (temperaturerounded >24.0)
        {
            System.out.println("Too High");
        }
    }
    //task 2
    //Write another routine that stores, in an array, the temperatures taken over a period of five hours. This routine calculates the difference between the highest temperature and the lowest temperature. Then it outputs the highest temperature, the lowest temperature, and the difference between these temperatures.
    public void task2()
    {
        System.out.print("Input Number of Hours: ");
        int hour = in.nextInt();
        //*12 because every 5 minutes and since there are 60 minutes in a hour
        // this means 60/5 = 12 hence 12* the number of hours inputted
        int loophours = hour*12 +1;
        //loops array, rounds input and sorts the array by ascending order
        //using Array.sort
        double[] tempstorage = new double [loophours];
        for (int i=1; i<loophours; i++)
        {
            System.out.println("Temp Count: " +i);
            double temperature = in.nextDouble();
            double temperaturerounded = Math.round(temperature * 10) / 10.0;
            tempstorage[i] = temperaturerounded;
            Arrays.sort(tempstorage);
        }
        //prints the positions 1 (lowest temperature) and loophours (highest temperature)
        for (int i=1; i<loophours; i++)
        {
            System.out.println(tempstorage[i]);
        }
        double min = tempstorage[1];
        System.out.println(min);
        double max = tempstorage[loophours-1];
        System.out.println(max);
        double difftemp = max - min;
        System.out.println("Highest Temperature: "+max);
        System.out.println("Lowest Temperature: "+min);
        System.out.println("Difference between Temperatures: "+difftemp);
    }
}

0 个答案:

没有答案