I don’t understand an ordination resolution

Asked

Viewed 26 times

3

The teacher asked for the next exercise:

"Write a program that receives three scores (integer numbers); sort these scores using if instructions and write the three in ascending order on the screen."

I tried to do it my own way but I ended up curled up for almost 30 minutes lost in the middle of so many if’s and E-e-s and went to see the resolution that the teacher made available, but I can’t understand it. The resolution is as follows::

System . out . println("Indique a 1ª pontuação:");
    int pontuacao1=scanner.nextInt();
    System . out . println("Indique a 2ª pontuação:");
    int pontuacao2 = scanner.nextInt();
    System . out . println("Indique a 3ª pontuação:");
    int pontuacao3 = scanner.nextInt();

     if (pontuacao2 < pontuacao1) 
    { 
        int valor = pontuacao1; 
        pontuacao1 = pontuacao2; 
        pontuacao2 = valor; 
    } 
    if (pontuacao3 < pontuacao2) 
    { 
        int valor = pontuacao2; 
        pontuacao2 = pontuacao3; 
        pontuacao3 = valor; 
        if (pontuacao2 < pontuacao1) 
        { 
            int valor2 = pontuacao1; 
            pontuacao1 = pontuacao2; 
            pontuacao2 = valor; 
        } 
    } 

    System.out.print(pontuacao1 + ", " + pontuacao2 + ", " + pontuacao3);

The first part of input values I understand but from the if is not. Someone can explain?

1 answer

3


Temporary variable

The variable valor serves to temporarily store a values not to lose it.

Come on,

We receive three values, let’s assume for example: 11, 5, 3.

Now we need to order them, so let’s make the comparisons with the conditional structures if:

if (pontuacao2 > pontacao1): Let’s compare the second score is higher than the first. In our case not right? Then let’s ask the next if,

if (pontuacao3 < pontuacao2): Is the third score lower than the second? YES. Then we enter the structure,

Since the last position is smaller than the second one they should be changed right? Yes. But if we just do:

pontuacao2 = pontuacao3;
pontacao3 = pontuacao2;

What will happen? Let’s think:

// pontuacao2 will receive the value of pontuacao3, soon pontuacao2 will turn 3,

// Now pontuacao3 receives the value of pontuacao2, but understand, pontuacao2 now is 3, then the two variables will end up having the value 3.

Where did our 5? In that case we lose it. So from a temporary variable, so we can temporarily hold the 5 to give him to the other pontuacao. Let’s see:

int valor = pontuacao2; 
pontuacao2 = pontuacao3; 
pontuacao3 = valor; 

// Now we temporarily store the value of pontuacao2 in valor, in that case, 5

// Then we pass the value of pontuacao3 for pontuacao2, she gets the 3 // Finally, we still have our 5 guarded, in valor, now we just pass it on to pontuacao3. Ready.

Just repeat this for the next condition: pontuacao2 < pontuacao1, which in this case is also true.

Now let’s think about the structure of if

We have first a question, if point 2 < point 1. We must ask this question, in the case of true condition, we enter the if and reverse the positions.

Now we have another question, if point 3 < point 2, why in this case we use another if instead of a else if? We use another if because we want to carry out this check regardless of the outcome of the first.

Now, why the third if is within the second? Note, this condition will ONLY be tested if the second is valid. If point 2 is not less than point 1 and point 3 is not less than point 2 means that the numbers are already sorted, so performing a third check would be unnecessary, so we end up optimizing our code.

Browser other questions tagged

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