Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何找到变量集的最大值_Java_Variables_Max - Fatal编程技术网

Java 如何找到变量集的最大值

Java 如何找到变量集的最大值,java,variables,max,Java,Variables,Max,我想知道是否有人能帮我找到一组变量的最大值并将它们分配给另一个变量。下面是我的一段代码,可能有助于理解我所说的内容 // 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 qu

我想知道是否有人能帮我找到一组变量的最大值并将它们分配给另一个变量。下面是我的一段代码,可能有助于理解我所说的内容

// 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 + ".");
。。。依此类推,在Java中,您可以这样使用:

double maxStock = Math.max( firstQuarter, Math.max( secondQuarter, Math.max( thirdQuarter, fourthQuarter ) ) );
DoubleStream.of(firstQuarter , secondQuarter , thirdQuarter , fourtQuarter).max();
不是最优雅的,但它会起作用

或者,对于更健壮的解决方案,请定义以下函数:

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

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

   return max;
}
您可以通过以下方式调用:

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

对于基元变量,最好的选择可能是数组或集合:

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

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

List<Double> data = new Arraylist<Double>();
data.add(firstQuarter);
data.add(secondQuarter);
data.add(thirdQuarter);
data.add(foutQuarter);
Collections.sort(data);
data.get(3);
要获得最大值,请执行以下操作:

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());
    }
}
List List=new ArrayList();
季度第一次=新季度();
first.setValue(第一季度);
第一,设定顺序(1);
列表。添加(第一);
//对其他值执行相同的操作
Collections.sort(list,newcomparator(){
比较(对象o1、对象o2){
返回Double.valueOf(o1.getValue()).compareTo(o2.getValue());
}
}

这可能更复杂,但如果您使用对象,我认为最好使用。

一种方法来管理所有对象

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

}
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;
}
公共静态T最大值(T…值){
如果(值为0.0)
m=值[i];
}
返回m;
}

聚会有点晚了,但是对于其他任何看到这个问题的人来说,Java8有一些基本流类型,它们正好实现了这一点

Collection<Integer> values = new ArrayList<>();
OptionalInt max = values.stream().mapToInt((x) -> x).max();
Collection values=new ArrayList();
OptionalInt max=values.stream().mapToInt((x)->x.max();
mapToInt
是描述如何将输入转换为整数类型的关键函数。生成的流具有特定于整数类型的附加聚合器和收集器,其中一个是
max()


然后可以从
optionant
中提取结果值,如果操作成功

我相信现在在Java 8中最简洁的版本如下所示:

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

我很抱歉,我以为这是在Java论坛或类似的网站上。@CodyBennett不用担心。我已经为您添加了Java标记。如果您正在寻找不同的答案,它可能会帮助您获得更多响应。此解决方案已被破坏为书面形式。Double.MIN\u值返回“Double类型的最小正非零值”,这是一个非常接近于0的数字,不是最大可能的负双精度。相反,可以使用double.negative_INFINITY或-double.MAX_VALUE。这是java8之前的一个好方法。对于现代方法,请参见@arjabbart的(下一个)答案。这应该是公认的解决方案