Greater and lesser number

Asked

Viewed 3,727 times

4

I need to sort three numbers I can’t use for and neither vectors.

I know it’s easy but I can’t do it. My problem is, I can’t keep the highest value.

     public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int menor = 0;
    int maior = 0;
    System.out.println(" Tres números:");
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();



    if((a < b) && (a < c))
        menor = a;

    else if((b < a)&&(b < c))
        menor = b;

    else if((c < a)&&(c < b))
        menor = c;

    System.out.println(" Maior: " + maior + " Menor:" + menor);
}

Oops I think I got it right.

if((a < b) && (a < c))
        menor = a;

    else if((b < a)&&(b < c))
        menor = b;

    else if((c < a)&&(c < b))
        menor = c;

    if((a > b) && (a > c))
        maior = a;

    else if((b > a)&&(b > c))
        maior = b;

    else if((c > a)&&(c > b))
        maior = c;
    System.out.println(" Maior: " + maior + " Menor:" + menor);
}
  • switch case can?

  • Yeah. But I think I figured it out, look if it’s right?

2 answers

3


A simple way would be this:

import java.util.Scanner;

class Ideone {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println(" Tres números:");
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int menor = a;
        int maior = a;
        if (b > maior) maior = b;
        if (c > maior) maior = c;
        if (b < menor) menor = b;
        if (c < menor) menor = c;
        System.out.println(" Maior: " + maior + " Menor: " + menor);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Obviously there are other ways to do this, but this is simple.

  • What is Integer.MAX_VALUE;?

  • I was out of my mind for not being able to immediately draw a logic for this without using loop, I even made diagram with < e > to understand and the solution was so simple kkk I feel an inciante

  • @Alinegonzaga I simplified and no longer need it. It would be the constant with the highest value of an integer.

  • I understand the way you did it, bigown. I like it. the variables will modify...

  • I don’t remember, but in java if the input is 123 456 789, he assigns direct the three variables or he only considers the first?

  • @Guilhermelautert He separates into three separate entries.

  • @Guilhermelautert, in this case, the blank will be the delimiter. Therefore, each integer will be for each of the variables.

Show 2 more comments

3

Another option would be:

import java.util.Scanner;

class Ideone {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println(" Tres números:");
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int menor;
        int maior;

        maior = Math.max(b,Math.max(c,a));
        menor = Math.min(b,Math.min(c,a));

        System.out.println(" Maior: " + maior + " Menor: " + menor);
    }
}

The expression below uses the function Math.max to get the highest value between two values. First, compare c with a. Then you compare the result of that with b. In the end, the greatest among these three will be returned.

Math.max(b,Math.max(c,a))

The same is true to get the smallest, however, using the function Math.min.

  • How difficult. complicated

  • I think I get it. Actually, it’s not that hard

  • @Alinegonzaga is simple, I just did not do so because I think it is not objective of the exercises to use ready things, is to test your ability to find the solution and I answered how to simplify what I had already found.

  • The Math class is your friend in Java (just like it exists in other languages). You will use it for various things.

  • 1

    Thanks. You guys are cool.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.