2017-02-04 1 views
0

私は絶対初心者です。 私は "if"を使ってmaxを見つける方法を知っていますが、私は方法を使ってそれをやりたかったのです。問題は、どのように私は最後のマックスの記録を保持することができます!ユーザーの最大入力を見つけるためのJAVAメソッドの作成方法

public class Max 
{ 

    public static double Max (double score){ 
     double max = 0; 
     if (score > max) 
      max = score; 
     return max; 
    } 

    public static void main (String [] arg){ 
     Scanner scan = new Scanner (System.in); 
     double score = 0; 

     do{ 
      System.out.print("Enter the scores (-1 to end input) > "); 
      score = scan.nextDouble(); 

     }while (score >= 0); 

     System.out.println("Highest score: " + Max(score)); 
    } 
} 
+0

私の答えを受け入れたとマークすることを忘れないでください – Developer

答えて

2

次の操作を行うが、あなたの方法を少し変更する可能性:

public static double Max (double score, double max){ 
    if (score > max) 
     max = score; 
    return max; 
} 

public static void main (String [] arg){ 
    Scanner scan = new Scanner (System.in); 
    double score = 0; 
    double max = 0; 

    do{ 
     System.out.print("Enter the scores (-1 to end input) > "); 
     score = scan.nextDouble(); 
     max = Max(score, max); 

    }while (score >= 0); 

    System.out.println("Highest score: " + Max(score)); 
} 

基本的に、あなたの関数は、2つの引数の最大を返します。現在の最大値を入力と比較し続け、高い数値を変数maxとして設定します。あなたは私の例ではそれは関係なく、あなたが使うアプローチのdouble max = 0;に作られ(ホルダー変数が必要になります。

0

あなたはこれを試すことができます。

public class Max { 

    public static double max(List<Double> score) { 
     double max = 0; 

     for (int i = 0; i < score.size(); i++) { 
      if (score.get(i) > max) { 
       max = score.get(i); 
      } 
     } 

     return max; 
    } 

    public static void main(String[] arg) { 
     Scanner scan = new Scanner(System.in); 
     List<Double> scores = new ArrayList<>(); 

     Double score; 
     do { 
      System.out.print("Enter the scores (-1 to end input) > "); 
      score = scan.nextDouble(); 
      scores.add(score); 
     } while (score >= 0); 

     System.out.println("Highest score: " + max(scores)); 
    } 
} 
0

あなたはListに入力値を保存する必要があります。そして、前方に置くのアイデアから離れて

public static double max(List<Double> score) { 
    double max = 0; 

    for (int i = 0; i < score.size(); i++) { 
     if (score.get(i) > max) { 
      max = score.get(i); 
     } 
    } 

    return max; 
} 

public static void main(String[] arg) { 
    Scanner scan = new Scanner(System.in); 
    List<Double> scores = new ArrayList<>(); 

    Double score; 
    do { 
     System.out.print("Enter the scores (-1 to end input) > "); 
     score = scan.nextDouble(); 
     scores.add(score); 
    } while (score >= 0); 

    System.out.println("Highest score: " + max(scores)); 
} 
0

MAXメソッドの入力パラメータとしてListオブジェクトを渡し、値が0より小さい場合、それはそれを比較したりするときにリストに追加していることに注意してくださいそうではないはずです。例えば、子供を見つけることが望まれる場合、失敗するでしょう。 より大きいまたは小さいものを見つけることに加えて、ストリームに頼ることができます。 Max()

public static double Max (List<Double> scores){ 
    return scores.stream().mapToDouble(i -> i).max().getAsDouble(); 
} 
public static void main(String[] args) { 
    Scanner t = new Scanner(System.in); 
    List<Double> scores = new ArrayList<>(); 
    double value=0; 
    while(true){ 
     value = t.nextDouble(); 
     if(value<=0) break; 
     else scores.add(value); 
    } 
    System.out.println(Max(scores)); 
} 
関連する問題