Exercise should show a result but shows 3

Asked

Viewed 97 times

-2

An algorithm that reads 3 integer values, not repeated, and shows in descending order. But it is showing 3 results, and should only be 1

package praticando;

import java.util.Scanner;


public class Exercicio2 {


    public static void main(String[] args) {


      Scanner valor = new Scanner(System.in);

        System.out.println("Digite o primeiro número: ");
        int A = valor.nextInt();

        System.out.println("Digite o segundo número: ");
        int B = valor.nextInt();

        System.out.println("Digite o terceiro número: ");
        int C = valor.nextInt();

        if (A == B || B == C || A== C) {
        System.out.println("Existem números repetidos!");
    } 
         if (A > B & A > C) { //A é o maior
            } if(B > C){
              System.out.println(+A+" -> "+B+" -> "+C);
            } else {
              System.out.println(+A+" -> "+C+" -> "+B);
        }

          if (B > A & B > C) { //B é o maior
            }  if(A > C){
              System.out.println(+B+" -> "+A+" -> "+C);
            } else {
              System.out.println(+B+" -> "+C+" -> "+A);
        }

        if (C > A & C > B) { //C é o maior
        } if(A > B){
              System.out.println(+C+" -> "+A+" -> "+B);
         }else{
              System.out.println(+C+" -> "+B+" -> "+A);
        }


    } 

}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

There’s some weird stuff in the code. I think I wanted to use && and not &. And I don’t understand why you make a if that opens and closes without executing anything there. Solved this works perfectly. If you had indented correctly you would realize that there is something wrong. I always say that whim when writing the code is important, but in general many people ignore it, think it has some gain to write anyway, when it is the opposite. See how neat it gets easier to see the error, take your code and organize better.

import java.util.Scanner;

class Exercicio2 {
    public static void main(String[] args) {
        Scanner valor = new Scanner(System.in);
        System.out.println("Digite o primeiro número: ");
        int A = valor.nextInt();
        System.out.println("Digite o segundo número: ");
        int B = valor.nextInt();
        System.out.println("Digite o terceiro número: ");
        int C = valor.nextInt();
        if (A == B || B == C || A == C) {
            System.out.println("Existem números repetidos!");
        } 
        if (A > B && A > C) { //A é o maior
            if (B > C) System.out.println(A + " -> " + B + " -> " + C);
            else System.out.println(A + " -> " + C + " -> " + B);
        }
        if (B > A && B > C) { //B é o maior
            if (A > C) System.out.println(B + " -> " + A + " -> " + C);
            else System.out.println(B + " -> " + C + " -> " + A);
        }
        if (C > A && C > B) { //C é o maior
            if (A > B) System.out.println(C + " -> " + A + " -> " + B);
            else System.out.println(C + " -> " + B + " -> " + A);
        }
    } 
}

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

You have to optimize this, but I’m not going to touch this part, it’s very simple, stay exercise.

  • Hi, about the indentation, I apologize, I’m learning. In the case of if, I thought it was necessary to close one if{} to open another, thank you for clarifying that. The main question is that

  • ...yes, it’s running. The problem is it’s showing me 3 results. 2 incorrect

  • 1

    You must open and close where it makes sense for what you want. I answered what you asked, if there are other mistakes should be in the question or ask new question, showing the error, detailing the problem.

  • Because that is exactly what I asked you the first time, dear friend. Because you are returning 3 instead of an answer. The correct one.

  • 1

    You’re returning one and I’ve demonstrated it.

  • Thanks for your help.

Show 1 more comment

Browser other questions tagged

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