dimanche 27 novembre 2016

Use of the if statement

I'm beginner in Java, and I'm learning the if statement without else, can please the community check my code to improve?

/* Code made by edu.

In this code we use only the if statement to practice. Here you need to enter your age to enter to the bar with hot girls, if your age is above to 18, you can enter. if it is below 18 you can't enter.

*/

import java.util.Scanner;

public class Main { public static void main(String[] args){

    Scanner input = new Scanner(System.in);

    int age;

    // Ask the age
    System.out.println("Please write your age to enter to the bar:");
    age = input.nextInt();

    // Equal to
    if(age == 18){
        System.out.println("Your age " + age + " it's equal to 18, you can enter");
    }

    // Not equal to
    if(age != 18){
        System.out.println("Your age " + age + " it's not equal to 18");
    }

    // Greater than
    if(age > 18){
        System.out.println("Your age " + age +  " " + "is greater than 18, you can enter");
    }

    // Less than
    if(age < 18){
        System.out.println("Your age " + age + " " + "is less than 18, you can't enter");
    }

    // Greater than or equal to
    if(age >= 18){
        System.out.println("Your age " + age + " is greater than or equal to 18, you can enter");
    }
    // Less than or equal to
    if(age <= 18){
        System.out.println("Your age " + age + " is less than or equal to 18, you can't enter");
    }
}

}

Aucun commentaire:

Enregistrer un commentaire