需要在没有数组的情况下获得最高和最低值

时间:2013-11-21 03:24:41

标签: java max min

我正在完成一项任务,我必须每周工作7天,每次都作为变量并创建两个函数。一个是找到7天的平均温度,另一个是找到7天最冷和最热的温度。我们不允许使用数组。我知道这是很多无用的代码,但是它的作用要求是什么。

我的问题是我可以弄清楚如何找到最小值和最大值。有什么建议吗?

import java.util.Scanner;

public class Temperature {
public static void getAverage(double day1, double day2, double day3, double day4, 
                              double day5, double day6, double day7){
double average = (day1 + day2 + day3 + day4 + day5 + day6 + day7)/7;

System.out.printf("The average is: %.2f\n", average);
}
public static void getHotAndCold( double day1, double day2, double day3, double day4, 
                               double day5, double day6, double day7){

}

public static void main(String [] args){
Scanner s = new Scanner(System.in);
double day1, day2, day3, day4, day5, day6, day7;

System.out.println("Enter the temperatures for each day of the week starting with,     Monday");
day1 = s.nextDouble();

System.out.println("Tuesday");
day2 = s.nextDouble();

System.out.println("Wednesday");
day3 = s.nextDouble();

System.out.println("Thursday");
day4 = s.nextDouble();

System.out.println("Friday");
day5 = s.nextDouble();

System.out.println("Saturday");
day6 = s.nextDouble();

System.out.println("Sunday");
day7 = s.nextDouble();

getAverage(day1, day2, day3, day4, day5, day6, day7);
 }
}

4 个答案:

答案 0 :(得分:1)

你可以这样做。使用Math.max来获得一对的最大值,然后继续这样做直到你到达第七天。最后一次比较给出了全局变量的值。

double max;
double min;

public static void getMaxAndMin(
                   double day1, double day2, double day3, double day4, 
                                double day5, double day6, double day7){

    max = Math.max(day1, day2);
    max = Math.max(max, day3);
    max = Math.max(max, day4);
    max = Math.max(max, day5);
    max = Math.max(max, day6);
    max = Math.max(max, day7);

    min = Math.min(day1, day2);
    min = Math.min(min, day3);
    min = Math.min(min, day4);
    min = Math.min(min, day5);
    min = Math.min(min, day6);
    min = Math.min(min, day7);

}

答案 1 :(得分:0)

您可以执行以下操作:

public static void getHotAndCold( double day1, double day2, double day3, double day4, 
                               double day5, double day6, double day7)
{
    double min = day1;
    double max = day1;

    if (day2 < min)
    {
        min = day2;
    }
    if (day2 > max)
    {
       max = day2;
    }

    // and so on.
}

答案 2 :(得分:0)

如果你真的想要循环(或只是恶意):

// create your own node to link days
public static class Day{
    double temp;
    Day next;
    Day(double temp, Day next){
        this.temp = temp;
        this.next = next;
    }
}

public static double coldest(Day day){
    double  minTemp = day.temp;
    while((day = day.next) != null) minTemp = Math.min(day.temp, minTemp);
    return minTemp;
}

//etc...

public static void main(String... args){
    Day days = new Day(70, new Day(60, new Day(65, new Day(45,
               new Day(83, new Day(72, new Day(55,null)))))));

    System.out.println("Coldest:" + coldest(days));
}

答案 3 :(得分:0)

其他答案的变体。

double max, min;

public static void getMaxMin(double day1, double day2, double day3, double day4,
                             double day5, double day6, double day7){

    max = Math.max(day1, Math.max(day2, Math.max(day3, Math.max(day4,
          Math.max(day5, Math.max(day6, day7))))));

    min = Math.min(day1, Math.min(day2, Math.min(day3, Math.min(day4,
          Math.min(day5, Math.min(day6, day7))))));

}