Receive 3 random values and allocate in 3 variables from smallest to largest

Asked

Viewed 118 times

1

I need to write a program that receives 3 random values and allocate the 3 values in 3 MINOR, INTERMEDIATE and MAJOR variables. I tried something like this, but when 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);

2 answers

2


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

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

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

Taking advantage, starting Java variables with uppercase letter is not part of the standard language. Prefer to start variable names in minuscule letter.

2

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. For this:

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

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

    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 the 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 highest. To finish just 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 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]);
    }
}

Browser other questions tagged

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