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 if
s. I’m testing on the 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);
}
}
Please avoid long discussions in the comments; your talk was moved to the chat
– Maniero