Algorithm problem

Asked

Viewed 77 times

0

Statement: Read 3 integers and order them in ascending order. At the end, show the values in ascending order, a blank line and then the values in sequence as read.

public class ex10 {
public static void main(String[] args){
    Scanner x = new Scanner(System.in);

    System.out.println("Digite 3 valores em sequencia:");
    int v1 = x.nextInt();
    int v2 = x.nextInt();
    int v3 = x.nextInt();

    int um = 0;
    int dois = 0;
    int tres = 0;

    if(v1 < v2 && v1 <v3){
        um = v1;
    }if(v2 < v3){
        dois = v2;
        tres = v3;
    }else{
        tres = v2;
        dois = v3;
    }
    if(v2 < v1 && v2 < v3){
        um = v2;
    }if(v1 < v3){
        dois = v1;
        tres = v3;
    }else{
        dois = v3;
        tres = v1;
    }
    if(v3 < v2 && v3 < v1){
        um = v3;
    }if(v2 < v1){
        dois = v2;
        tres = v1;
    }else{
        dois = v1;
        tres = v2;
    }
    System.out.println("");
    System.out.println(um);
    System.out.println(dois);
    System.out.println(tres);
    System.out.println("");
    System.out.println(v1);
    System.out.println(v2);
    System.out.println(v3);


}

}

My problem is this, when the user writes the numbers from the largest to the smallest (6,5,4) or from the smallest to the largest (4,5,6) the algorithm works correctly, but when the user puts out of order, for example 6,4,5 goes wrong. Another question is whether it would be possible to do this algorithm in a more summarized way.

  • Joao, post the solution as an answer, adding to the question you leave her confused.

  • Okay, I’ll do it!

  • Possible duplicate of Greater and lesser number

2 answers

2

import java.util.Scanner;

public class Ex10 {
    public static void main(String[] args) {
        Scanner x = new Scanner(System.in);

        System.out.println("Digite 3 valores em sequencia:");
        int a = x.nextInt();
        int b = x.nextInt();
        int c = x.nextInt();
        int v1 = a, v2 = b, v3 = c;

        // Se v1 for maior que v2, troca eles de lugar.
        if (v1 > v2) {
            int aux = v2;
            v2 = v1;
            v1 = aux;
        }

        // Se v1 for maior que v3, troca eles de lugar.
        if (v1 > v3) {
            int aux = v3;
            v3 = v1;
            v1 = aux;
        }

        // Se v2 for maior que v3, troca eles de lugar.
        if (v2 > v3) {
            int aux = v3;
            v3 = v2;
            v2 = aux;
        }

        System.out.println(v1 + ", " + v2 + ", " + v3);
        System.out.println();
        System.out.println(a + ", " + b + ", " + c);
    }
}

See here working on ideone.

The idea is to get v1 has the lowest value. For this compares with v2 and v3, changing case v1 is not the least. Therefore, after these two steps, v1 will be the smallest number.

Next, v2 must be the middle number and v3 the largest. Then just compare them and exchange them if not so.

Note that you do not need to show each value separately from each other in your System.out.println. You can match an entire row in each System.out.println.

0

resolution:

public class ex10 {
public static void main(String[] args){
    Scanner x = new Scanner(System.in);

    System.out.println("Digite 3 valores em sequencia:");
    int v1 = x.nextInt();
    int v2 = x.nextInt();
    int v3 = x.nextInt();

    int um = 0;
    int dois = 0;
    int tres = 0;

    if(v1 < v2 && v1 <v3){
        um = v1;
    if(v2 < v3){
        dois = v2;
        tres = v3;
    }else{
        dois = v3;
        tres = v2;
    }
    }
    if(v2 < v1 && v2 < v3){
        um = v2;
    if(v1 < v3){
        dois = v1;
        tres = v3;
    }else{
        dois = v3;
        tres = v1;
    }
    }
    if(v3 < v2 && v1 > v3){
        um = v3;
    if(v2 < v1){
        dois = v2;
        tres = v1;
    }else{
        dois = v1;
        tres = v2;
    }
    }
    System.out.println("");
    System.out.println(um);
    System.out.println(dois);
    System.out.println(tres);
    System.out.println("");
    System.out.println(v1);
    System.out.println(v2);
    System.out.println(v3);


}

}

my mistake was having locked the key too early in that part:

if(v1 < v2 && v1 <v3){
    um = v1;
}if(v2 < v3){
    dois = v2;
    tres = v3;

I closed just before to put the second if, when I should have left open

if(v1 < v2 && v1 <v3){
    um = v1;
if(v2 < v3){
    dois = v2;
    tres = v3;
  • Can you elaborate on what you’ve done? Code-only responses are not very useful to the community and usually add nothing.

  • @Andersoncarloswoss Could you give me the link of those answers on the Soen that you said?

  • @Victorstafusa https://stackoverflow.com/a/20600020/1452488, https://stackoverflow.com/a/19651476/1452488, https://stackoverflow.com/a/16826296/1452488. In some cases there is indeed some comment on, but the consensus seems to be that it is ok to use. However, I don’t use Java, so I may have misunderstood.

  • 1

    @Andersoncarloswoss No, that’s a pretty hideous gambiarra. It’s not consensus that this is ok to use, it’s actually consensus that this nay should never be used.

  • @Andersoncarloswoss will do it!

Browser other questions tagged

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