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