Variables and structure of flow control if in Java

0

The user is asked to enter 3 numbers and the end line is " The largest value is X the middle value is Y and the smallest value is Z ". Where X, Y and Z are the numbers entered. I can only do with if s. I'm testing on cmd.

How could I make the code below only with if s 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);
    }
}
    
asked by anonymous 28.02.2018 / 16:44

2 answers

1

The problem that the compiler is showing on the terminal

System.out.println ("The highest value Ú" + largest + "means value Ú" + middle + "and the lowest value Ú" + minor);

You did not initialize the major, middle, and lower variables.

The right thing to do would be:

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

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

If you want, you can also declare your variables as static outside 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);
.....
    
28.02.2018 / 17:20
1

You can do with three if s 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:

  • Initially, set the menor , meio , and maior values to the given values in any order.

  • Verify that the value of menor is actually less than the initial value of meio and that of maior and change them if it is not. This will ensure that the menor variable will have the lowest of the three values. This can be done with two if s.

  • Verify that the value of meio and maior are reversed and change them if necessary.

  • 28.02.2018 / 17:46