Ali Tahrei Sh.
Ali Tahrei Sh.

Reputation: 11

How to write a JAVA method to find the Maximum of users input?

I'm an absolute beginner. I know how to find the max with "if", but I wanted to do it using a method. The problem is how can I keep the record of the last Max!

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));
    }
}

Upvotes: 0

Views: 631

Answers (4)

DarkFenix
DarkFenix

Reputation: 746

Apart from the ideas put forward, be careful that if the value is less than 0 it compares it or adds it to the list when it should not be so, in case for example that it is desired to find the child, it would fail. In addition to finding the greater or lesser one could resort to the streams. 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));
}

Upvotes: 0

TOP
TOP

Reputation: 2614

You have to save the input values in a List. Then pass the List object as input parameter of MAX method.

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));
}

Upvotes: 0

Aaric Chen
Aaric Chen

Reputation: 1188

You can try this.

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));
    }
}

Upvotes: 0

Developer
Developer

Reputation: 361

You could do the following, but modify your method a little:

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));
}

Basically your function returns the Max of the two arguments. It will keep comparing the current max to the input and setting the higher number as the variable max. You will need a holder variable (in my example it is made in double max = 0; regardless of the approach you use.

Upvotes: 2

Related Questions