2017-02-04 35 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)); 
} 

基本上你的函數返回兩個參數的最大值。它將繼續比較當前最大值與輸入值,並將較高數字設置爲變量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的輸入值。然後,通過List對象作爲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

除了提出的思想,請注意如果該值小於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)); 
} 
相關問題