2012-02-16 31 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

に、あなたはこのように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

私はこれを考えた謝罪ジャワのフォーラムの下にあったか、それに沿ったものでした。 –

+0

@CodyBennett心配はいりません。私はあなたのためのJavaタグを追加しました。あなたが別の答えを探しているなら、それはもっと多くの反応を得る助けになるでしょう。 – nybbler

+0

この解決方法は書かれているとおりに破損しています。 Double.MIN_VALUEは、 "double型の最小正のゼロ以外の値"を返します。これは0に非常に近い数値であり、可能な限り大きな負の倍数ではありません。代わりに、Double.NEGATIVE_INFINITYまたは-Double.MAX_VALUEのようなものを使用できます。 – ulmangt

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

...だからJavaでは

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 
} 

とMAX値を取得するには:

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は、入力を整数型に変換する方法を説明する重要な関数です。結果として得られるストリームには、整数型に固有の追加のアグリゲータとコレクタがあり、その1つはmax()です。

操作は、私が最も簡潔なバージョンは次のようになりますのJava 8で今信じる

1

成功した場合、結果の値は、その後、OptionalIntから抽出することができます。

DoubleStream.of(firstQuarter , secondQuarter , thirdQuarter , fourtQuarter).max(); 
関連する問題