How to find maximum value of set of variables

2019-01-12 05:56发布

问题:

I was wondering if anyone could help me find the maximum value of a set of variables and assign them to another variable. Here is a snippet of my code that may help with understanding what I am talking about.

// Ask for quarter values.
    System.out.println("What is the value of the first quarter?");
    firstQuarter = input.nextDouble();

    System.out.println("What is the value of the second quarter?");
    secondQuarter = input.nextDouble();

    System.out.println("What is the value of the third quarter?");
    thirdQuarter = input.nextDouble();

    System.out.println("What is the value of the fourth quarter?");
    fourthQuarter = input.nextDouble();

    //Tell client the maximum value/price of the stock during the year.     
    //maxStock = This is where I need help 
    System.out.println("The maximum price of a stock share in the year is: $" + maxStock + ".");

回答1:

In Java, you can use Math.max like this:

double maxStock = Math.max( firstQuarter, Math.max( secondQuarter, Math.max( thirdQuarter, fourthQuarter ) ) );

Not the most elegant, but it will work.

Alternatively, for a more robust solution define the following function:

private double findMax(double... vals) {
   double max = Double.NEGATIVE_INFINITY;

   for (double d : vals) {
      if (d > max) max = d;
   }

   return max;
}

Which you can then call by:

double maxStock = findMax(firstQuarter, secondQuarter, thirdQuarter, fourthQuarter);


回答2:

One method to rule them all

public static <T extends Comparable<T>> T max(T...values) {
    if (values.length <= 0)
        throw new IllegalArgumentException();

    T m = values[0];
    for (int i = 1; i < values.length; ++i) {
        if (values[i].compareTo(m) > 0)
            m = values[i];
    }

    return m;
}


回答3:

With primitive variables the best choice maybe with Arrays or Collections:

Arrays:

double [ ] data = { firstQuarter , secondQuarter , thirdQuarter , fourtQuarter ) ;
Arrays . sort ( data ) [ 3 ] ;

Collections:

List<Double> data = new Arraylist<Double>();
data.add(firstQuarter);
data.add(secondQuarter);
data.add(thirdQuarter);
data.add(foutQuarter);
Collections.sort(data);
data.get(3);

And if you work with Objects you may use Collections with a Comparator:

class Quarter {
    private double value;
    private int order;

    // gets and sets
}

And to get the max value:

List<Quarter> list = new ArrayList<Quarter>();
Quarter fisrt = new Quarter();
first.setValue(firstQuarter);
first.setOrder(1);
list.add(first);
// Do the same with the other values
Collections.sort(list, new Comparator<Quarter>(){
    compare(Object o1, Object o2){
        return Double.valueOf(o1.getValue()).compareTo(o2.getValue());
    }
}

This may be more complex, but if you working with objects i think is better to work.



回答4:

double [ ] data = { firstQuarter , secondQuarter , thirdQuarter , fourtQuarter ) ;
Arrays . sort ( data ) [ 3 ] ;


回答5:

A little late to the party, but for anyone else viewing this question, java 8 has primitive stream types that implement exactly this

Collection<Integer> values = new ArrayList<>();
OptionalInt max = values.stream().mapToInt((x) -> x).max();

mapToInt is the key function that describes how to convert the input to an integer type. The resulting stream then has additional aggregators and collectors specific to integer types, one of the being max().

The result value can then be extracted from the OptionalInt, if the operation was successful



回答6:

I believe now in Java 8 the most concise version would look like this:

DoubleStream.of(firstQuarter , secondQuarter , thirdQuarter , fourtQuarter).max();


回答7:

Max = firstQuarter;
If(secondQuarter > max)
   Max = secondQuarter;

... And so on



回答8:

import java.util.Scanner;


public class dmar {

public static void main ( String  []  srgs){

    Scanner dmar=new Scanner(System.in);{

        {

System.out.println ( "inter number of x  plz") ;

double x=dmar.nextDouble();

System.out.println ( "inter number of y plz") ;{


double y=dmar.nextDouble();

System.out.println ( "inter number of t  plz") ;
double t=dmar.nextDouble();

System.out.println ( "inter number of f  plz") ;
double f=dmar.nextDouble();

{

{
if (x>y);

System.out.println("x biger than y");


if (x>t);
System.out.println("x biger than t");

 if (x>f);

System.out.println("x biger than f");


 if (y>x);
System.out.println("y biger than x");



if (y>t);
System.out.println("y biger than t");

 if (y>f);
System.out.println("y biger than f");

if (t>x&t>f&t>y);
System.out.println("t biger than x");

if (t>y);
System.out.println("t biger than y");

if (t>f);
System.out.println("t biger than f");

 if (f>x);
System.out.println("f biger than x");


if (f>y);
System.out.println("f biger than y");


         if (f>t);
System.out.println("f biger than t");

}