Decrease quatity of "if"

Asked

Viewed 39 times

0

Hello ! I made a Java code to answer an exercise and was curious to decrease the amount of "if" in order to improve the code,who can help. The questionnaire calls for. Develop a program that reads four integer numeric values and shows values that are divisible by 2 and 3.

public class ValorDivisivel{

    public static void main(String[] args){

        System.out.print("Digite 1° num: ");
        int num1 = Integer.parseInt(System.console().readLine());
        System.out.print("Digite 2° num: ");
        int num2 = Integer.parseInt(System.console().readLine());
        System.out.print("Digite 3° num: ");
        int num3 = Integer.parseInt(System.console().readLine());
        System.out.print("Digite 4° num: ");
        int num4 = Integer.parseInt(System.console().readLine());           

        System.out.print("Valor divisível por 2: ");
        if(num1 % 2 == 0){
            System.out.print(num1+"  ");        
        }
        if(num2 % 2 == 0){
            System.out.print(num2+"  ");        
        } 
        if(num3 % 2 == 0){
            System.out.print(num3+"  ");        
        }
        if(num4 % 2 == 0){
            System.out.print(num4+" ");     
        }
        if(num1 % 2 != 0 && num2 % 2 != 0 && num3 % 2 != 0 && num4 % 2 != 0){
            System.out.println("Não tem!!!");
        }
        System.out.print("Valor divisível por 3: ");
        if(num1 % 3 == 0){
            System.out.print(num1+"  ");        
        }
        if(num2 % 3 == 0){
            System.out.print(num2+"  ");        
        }
        if(num3 % 3 == 0){
            System.out.print(num3+"  ");        
        }
        if(num4 % 3 == 0){
            System.out.print(num4+" ");     
        }
        if(num1 % 3 != 0 && num2 % 3 != 0 && num3 % 3 != 0 && num4 % 3 != 0){
            System.out.println("Não tem!!!");
        }
        System.out.println();

    }

}
  • 2

    For more details: https://onlinegdb.com/Bkrfxo_qU

  • 1

    On Youtube have great explanations on this subject in Portuguese.

No answers

Browser other questions tagged

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