2012-02-16 62 views
12

我想知道是否有人能幫我找到一組變量的最大值並將它們賦值給另一個變量。這裏是我的代碼片段,可以幫助理解我在說什麼。如何找到變量集的最大值

// 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 + "."); 

回答

22

在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); 
+0

我很抱歉,我認爲這在Java論壇或類似的東西之下。 – 2012-02-16 00:55:50

+0

@CodyBennett不用擔心。我已經爲你添加了Java標籤。如果您正在尋找不同的答案,它可能有助於獲得更多答案。 – nybbler 2012-02-16 01:08:46

+0

該解決方案如書面所述被打破。 Double.MIN_VALUE返回「double類型的最小正非零值」,它是一個非常接近0的數字,而不是最大可能的負雙數。相反,可以使用類似Double.NEGATIVE_INFINITY或-Double.MAX_VALUE。 – ulmangt 2014-08-22 02:23:44

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

...等

1
double [ ] data = { firstQuarter , secondQuarter , thirdQuarter , fourtQuarter) ; 
Arrays . sort (data) [ 3 ] ; 
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); 

如果你使用對象,你可以使用集合了比較:

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

} 
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; 
} 
1

有點遲到了,但對於其他人觀看了這個問題,JAVA 8具有實現的正是這種

Collection<Integer> values = new ArrayList<>(); 
OptionalInt max = values.stream().mapToInt((x) -> x).max(); 
原始流類型

mapToInt是描述如何將輸入轉換爲整數類型的關鍵函數。生成的流然後具有特定於整數類型的其他聚合器和收集器,其中之一是max()

結果值然後可以從OptionalInt提取,如果操作成功

1

我現在相信用Java 8中的最簡潔的版本是這樣的:

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