Variables and if flow control structure in Java

Asked

Viewed 74 times

0

The intended is for the user to enter 3 numbers and for the sentence to appear at the end "The highest value is X the middle value is Y and the lowest value is Z". Where X, Y and Z are the numbers entered. I can only do with ifs. I’m testing on the cmd.

How could I make the code below only with ifs more abbreviated?

import java.util.Scanner;

public class P02IfsTest {
    public static void main(String [] args){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Introduza 3 números: ");

        int scan1 = keyboard.nextInt();
        int scan2 = keyboard.nextInt();
        int scan3 = keyboard.nextInt();

        int menor = 0;
        int meio = 0; 
        int maior = 0;

        if(scan1 > scan2 && scan1 > scan3 && scan2 > scan3) {
            maior = scan1;
            meio = scan2;
            menor = scan3;
        }
        if(scan1 > scan2 && scan1 > scan3 && scan3 > scan2) {
            maior = scan1;
            meio = scan3;
            menor = scan2;
        }
        if(scan2 > scan1 && scan2 > scan3 && scan1 > scan3) {    
            maior = scan2;
            meio = scan1;
            menor = scan3;
        }
        if(scan2 > scan1 && scan2 > scan3 && scan1 < scan3) {
            maior = scan2;
            meio = scan3;
            menor = scan1;
        }
        if(scan3 > scan1 && scan3 > scan2 && scan1 > scan2) {
            maior = scan3;
            meio = scan1;
            menor = scan2;
        }
        if(scan3 > scan1 && scan3 > scan2 && scan1 < scan2) {
            maior = scan3;
            meio = scan2;
            menor = scan1;
        }
        System.out.println("O maior valor é " + maior + " o valor do meio é " + meio + " e o menor valor é " + menor);
    }
}

2 answers

1

The problem the compiler is showing in the terminal

02IfsTest.java:47: error: variable maior Might not have been initialized System.out.println("The highest value Ú " + the highest value + " the middle value Ú " + half + " and the lowest value Ú " + the lowest);

is that Voce did not initialize the major, medium and minor variables.

the right thing to do would be :

int menor = 0;
int meio = 0;
int maior = 0;

The compiler cannot define the default value of its variables because they are declared within the main method which is a static method.

If you want Voce you can also declare your variables as static outside of the method.

public class Main {

static int menor;
static int meio;
static int maior;

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
.....
  • I already solved the bug. Thank you.

  • You would have to make an if for, for example, scan1 to be scan2 equal?

  • It is not possible to make it more abbreviated only with ifs?

  • Gustavo, I believe that it is not exactly right to initialize with the default value 0. It would not return more compilation error, but "conceptually" could have wrong values. The correct thing would be to ensure that the code will pass through one of the execution branches. And this would be done using the elses to perform the "other side" only if the condition is not met

  • 1

    @Jeffersonquesado Yes I understand your question, as I said in the reply I just tried to explain the error presented by the compiler and why of the error, because then she would understand the real reason for that error presented.

  • @Gustavodesouza understood his point now. In quick reading I had not understood that this was the intention, nor did I erase myself the way you spoke

Show 1 more comment

1

You can do it with three ifs like this:

import java.util.Scanner;

public class P02IfsTest {
    public static void main(String[] args){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Introduza 3 números: ");

        int scan1 = keyboard.nextInt();
        int scan2 = keyboard.nextInt();
        int scan3 = keyboard.nextInt();

        int menor = scan1, meio = scan2, maior = scan3;

        if (menor > meio) {
            int x = menor;
            menor = meio;
            meio = x;
        }

        if (menor > maior) {
            int x = menor;
            menor = maior;
            maior = x;
        }

        if (meio > maior) {
            int x = meio;
            meio = maior;
            maior = x;
        }

        System.out.println("O maior valor é " + maior
                + " o valor do meio é " + meio
                + " e o menor valor é " + menor);
    }
}

The idea is this:

  1. Initially, put the values menor, meio and maior as the values given, in any order.

  2. Check if the value of menor is in fact less than the initial value of the meio and that of maior and exchange them if it is not. This will ensure that the variable menor will be with the lowest of the three values. This from here can be done with two ifs.

  3. Check if the value of meio and of maior are reversed and change them if applicable.

  • But so I’m assuming that the sca1 is the smallest, etc

  • And the syntax is not the most correct

  • @Catarinamota Yes, that’s right. See here working on ideone.

  • @Catarinamota You take over only initially that the scan1 is the smallest. The ifs serve exactly to correct this initial presumption if it is false.

Browser other questions tagged

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