2017-02-04 9 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)); 
} 
相关问题