Receive 3 random values and allocate in 3 variables from the lowest to the highest

1

I need to write a program that receives 3 random values and allocates the 3 values to 3 variables MINOR, MIDDLE, and GREATER. I tried something like this, but at the time of printing the values, I get the error that var A B C were not initialized.

Error:

variable A might not have been initialized
variable B might not have been initialized
variable C might not have been initialized

Code:

int A,B,C;
System.out.println("Digite 3 valores!");
int vala = input.nextInt();
int valb = input.nextInt();
int valc = input.nextInt();

if        (vala > valb && vala > valc)  { A = vala;}
else if (valb > vala && valb > valc)  { A = valb;}
else if (valc > vala && valc > valc)  { A = valc;}
else if (vala < valb && vala < valc)  { B = vala;}
else if (valb < vala && valb < valc)  { B = valb;}
else if (valc < vala && valc < valb)  { B = valc;}
else if (vala > valb && vala < valc || vala < valb && vala > valc)  { C = vala;}
else if (valb > vala && valb < valc || valb < vala && valb > valc)  { C = valb;}
else if (valc > vala && valc < valb || valc < vala && valc > valb)  { C = valc;}

System.out.println(A);
System.out.println(B);
System.out.println(C);
    
asked by anonymous 10.04.2018 / 21:51

2 answers

1

You need to start the local variables A , B and C with some value. Probably in your case, with the value zero:

int A = 0, B = 0, C = 0;

If the variable is local and = 0 is omitted, then the variable has no value, not even zero . . The automatic assignment of int to zero will only apply to the fields of a class.

Taking advantage of starting the Java variable with a capital letter is not part of the language pattern. Prefer to start variable names in lowercase.

    
10.04.2018 / 22:26
1

The error code says it all

variable A might not have been initialized
variable B might not have been initialized
variable C might not have been initialized

That is, you did not initialize the variables. To do this:

int a=0,b=0,c=0; 

You can improve the code by using fewer comparisons and variables. We can use four variables, being a, b, c for the values and an aux to perform the value exchanges.

    int a,b,c,aux;

    Scanner input = new Scanner(System.in);

    System.out.print("Digite 3 valores!");
    a = input.nextInt();
    b = input.nextInt();
    c = input.nextInt();

The first step is to find the highest value among the three numbers.

    if (a > b) { // a é maior que b? trocamos seus valores.
        aux = a; // variável aux recebe o valor contido em a
        a = b;   // a recebe o valor de b
        b = aux; // b recebe o valor de aux, que contém o valor inicial de a
    }

We repeat the comparison so that variable c receives the highest value

    if (b > c) { // b é maior que c? trocamos seus valores
        aux = b; // variável aux recebe o valor contido em b
        b = c;   // b recebe o valor de c
        c = aux; // c recebe o valor de aux, que contém o valor inicial de b
    }

With these two comparisons we make sure that the value contained in c is the largest. To finish, simply adjust the values of a and b with the same initial comparison.

    if (a > b) { // a é maior que b? trocamos seus valores.
        aux = a; // variável aux recebe o valor contido em a
        a = b;   // a recebe o valor de b
        b = aux; // b recebe o valor de aux, que contém o valor inicial de a
    }

Finally, print the values.

    System.out.println("Menor: "+a);
    System.out.println("Intermediario: "+b);
    System.out.println("Maior: "+c);

Another much easier and more reusable way for various problems:

import java.util.Arrays;
import java.util.Scanner;

public class Teste {
    public static void main(String args[]) {
        int a,b,c;
        Scanner input = new Scanner(System.in);

        System.out.print("Digite 3 valores!");
        a = input.nextInt();
        b = input.nextInt();
        c = input.nextInt();

        int meuArray[] = {a,b,c}; //inicializa nosso array com os valores das 3 variáveis.

        Arrays.sort(meuArray); //ordena os valores 

        System.out.println("Menor: "+meuArray[0]);
        System.out.println("Intermediario: "+meuArray[1]);
        System.out.println("Maior: "+meuArray[2]);
    }
}
    
15.04.2018 / 18:42