2016-11-05 22 views
0

私は、異なる種類の温度でユーザーが入力できるようにするコードを作成しています。Javaで自分のコードで最大値と最小値を見つける

ユーザーが入力することができる必要があります:週間人は1週間内の温度を測定しているどのように多くの時間 の

額。 毎週の気温です。

ユーザーが情報を入力したときに、プログラムはで入力されている温度が表示されるはずですが。

プログラムは毎週最低温度を表示する必要があります。 プログラムは毎週最高の気温を表示する必要があります。 プログラムは、すべての週の間、最低温度と最高温度を表示する必要があります。 プログラムには、毎週、およびすべての週の平均気温が表示されます。

これは私が来てどのくらいです:

{ 
    System.out.println ("Temperatures\n"); 


    Scanner in = new Scanner (System.in); 
    in.useLocale (Locale.US); 

    //Here is where information is being put in 
    System.out.print ("amount of weeks: "); 
    int amountOfWeeks = in.nextInt(); 
    System.out.print ("measuring temperature per week: "); 
    int measurementsPerWeek = in.nextInt(); 

    //array to store the information 
    double[][] t = new double[amountOfWeeks + 1][measurementsPerWeek + 1]; 

    //mata in temperaturerna 
    for (int week = 1; week <= amountOfWeeks; week++) 
    { 
     System.out.println ("temperatures - week " + week + ":"); 
     for (int measurement = 1; measurement <= measurementsPerWeek; measurement++) 
      t[week][measurement] = in.nextDouble(); 
    } 
    System.out.println(); 

    //show the temperatures 
    System.out.println ("temperatures"); 
    for (int week = 1; week <= amountOfWeeks; week++) 
    { 
     for (int measurement = 1; measurement <= measurementsPerWeek; measurement++) 
      System.out.print (t[week][measurement] + " "); 

     System.out.println(); 
     } 

    //lowest, highest, the sum of all temperatures, and average temp per week 
    double[] minT = new double[amountOfWeeks + 1]; 
    double[] maxT = new double[amountOfWeeks + 1]; 
    double[] sumT = new double[amountOfWeeks + 1]; 
    double[] averageT = new double[amountOfWeeks]; 

    // write the code to find the lowest temperature for the first week 



    //´Lowest, highest, sum, average for all weeks 
    double minTemp = minT[1]; 
    double maxTemp = maxT[1]; 
    double sumTemp = sumT[1]; 
    double averageTemp = 0; 


} 

}

+0

import java.util。*; //スキャナ、ロケール クラス温度 { \tのpublic static無効メイン(文字列[] args)は具体的に私はちょうど私の中で最低温度を見つける方法を始めるためにいくつかの助けを必要とするコード –

+0

の初めにする必要がありますコード。 –

+0

あなたは 'DoubleSummaryStatistics'を見ることができます。 – Bubletan

答えて

0

をリスト内の最大値を見つけるための一般的なアルゴリズムは、リストを反復処理し、これまでに発生した最大値を格納することです。たとえば、intの配列に格納されている10個の数字がある場合:

int max=intArray[0]; 
for (int i=1; i<10;i++) 
     if (intArray[i]>max) 
      max=intArrray[i]; 
0

するOptionalDoubleを取得するために、[()またはMAX()、または平均】ストリームに配列を入れて分()メソッドを使用し値。

minValue.ifPresent(e -> System.out.println(e)); 

minValue.ifPresent(System.out::println); 

か、他の何かをしたい場合は、あなたが使用することができます:あなたはそれを印刷したい場合は

OptionalDouble minValue = Arrays.stream(myArray).min(); 

その後、ラムダ式やメソッドを参照してifPresent()を使用get()メソッド。

0

あなたが宣言することができます。

double min = Double.POSITIVE_INFINITY; 
double max = Double.NEGATIVE_INFINITY; 

を、あなたはあなたはそれが新しい最大または最小だかどうかをテストし、必要であれば、このようにそれらを更新温度読むたび:

... 
t[week][measurement] = in.nextDouble(); 
if (t[week][measurement] < min) min = t[week][measurement]; 
if (t[week][measurement] > max) max = t[week][measurement]; 
... 
0

を私は今、答えを見直しここに投稿し、これを私のコードに書いた:

double min = Double.MAX_VALUE; 

for (int week = 1; week<= amountOfWeeks ; week++) 
{ 
    for (int measurement = 1; measurement <= measurementsPerWeek; measurement ++) 
     if (t[week][measurement] < min) 
     min = t[week][measurement]; 

     System.out.println ("min is:" + min); 
    } 

コードは入力を通りますすべての入力のすべてのデータを週単位で検索し、最小数を見つけて印刷します。基本的に、最初の週の最初の温度が1で、ユーザーが1よりも低い値を入力しない場合、私のコードはこの数をすべての週の最小値として設定します。

これを変更して、毎週、毎週の入力を最小限に抑える方法を教えてください。

関連する問題