如何找到变量集的最大值
我想知道是否有人可以帮助我找到一组变量的最大值并将它们分配给另一个变量。 这是我的代码片段,可能有助于理解我在说什么。
// 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中,您可以像这样使用Math.max :
double maxStock = Math.max( firstQuarter, Math.max( secondQuarter, Math.max( thirdQuarter, fourthQuarter ) ) );
不是最优雅,但它会起作用。
或者,对于更强大的解决方案,定义以下function:
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);
一种统治它们的方法
public static > 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; }
对于原始变量,最好的选择可能是使用数组或集合:
arrays:
double [ ] data = { firstQuarter , secondQuarter , thirdQuarter , fourtQuarter ) ; Arrays . sort ( data ) [ 3 ] ;
类别:
List data = new Arraylist (); data.add(firstQuarter); data.add(secondQuarter); data.add(thirdQuarter); data.add(foutQuarter); Collections.sort(data); data.get(3);
如果你使用对象,你可以使用Collections与Comparator:
class Quarter { private double value; private int order; // gets and sets }
并获得最大值:
List list = new ArrayList (); 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 (){ compare(Object o1, Object o2){ return Double.valueOf(o1.getValue()).compareTo(o2.getValue()); } }
这可能更复杂,但如果你使用对象,我认为最好工作。
double [ ] data = { firstQuarter , secondQuarter , thirdQuarter , fourtQuarter ) ; Arrays . sort ( data ) [ 3 ] ;
派对有点晚了,但是对于其他任何观看这个问题的人来说,java 8都有原始的流类型,可以实现这一点
Collection values = new ArrayList<>(); OptionalInt max = values.stream().mapToInt((x) -> x).max();
mapToInt
是描述如何将输入转换为整数类型的关键函数。 然后,生成的流具有特定于整数类型的附加聚合器和收集器,其中一个是max()
。
如果操作成功,则可以从OptionalInt
提取结果值
我相信现在Java 8中最简洁的版本看起来像这样:
DoubleStream.of(firstQuarter , secondQuarter , thirdQuarter , fourtQuarter).max();
Max = firstQuarter; If(secondQuarter > max) Max = secondQuarter;
… 等等
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"); }