Regin
Regin

Reputation: 39

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?

Problem statement: 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");
        }
    }
}

Upvotes: 2

Views: 1230

Answers (5)

Enrico
Enrico

Reputation: 31

here a method that calculate if a user is adult respect now()

public Boolean isMaggiorenne(Date dataGenerica) {
Boolean isMagg = false;
if (dataGenerica != null && dataNascita != null) {

GregorianCalendar dataDiNascita = new GregorianCalendar();
dataDiNascita.setTime(dataNascita);

GregorianCalendar dataMagg = new GregorianCalendar();
dataMagg.setTime(dataGenerica);

int anno = dataMagg.get(GregorianCalendar.YEAR) – dataDiNascita.get(GregorianCalendar.YEAR);
int mese = dataMagg.get(GregorianCalendar.MONTH) – dataDiNascita.get(GregorianCalendar.MONTH);
int giorno = dataMagg.get(GregorianCalendar.DAY_OF_MONTH) – dataDiNascita.get(GregorianCalendar.DAY_OF_MONTH);

isMagg = anno>18 || (anno==18 && (mese>0 || (mese==0 && giorno>=0))) ;
}
return isMagg;
}

here there is max precision!

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Technically, you have just three cases:

  • age < 18
  • age == 18
  • age > 18

So you can simplify your code into

...
if (age < 18) {
  System.out.println("Your age " + age + " it's not equal to 18");
  System.out.println("Your age " + age + " is less than 18, you can't enter");
  System.out.println("Your age " + age + " is less than or equal to 18, you can't enter");
}

// "else if" instead of "if" will make the code more readable   
if (age == 18) {
  System.out.println("Your age " + age + " it's equal to 18, you can enter");
  System.out.println("Your age " + age + " is greater than or equal to 18, you can enter"); 
  System.out.println("Your age " + age + " is less than or equal to 18, you can't enter");
}   

// "else" instead of "if" will make the code more readable   
if (age > 18) {
  System.out.println("Your age " + age + " it's not equal to 18");
  System.out.println("Your age " + age + " is greater than 18, you can enter");
  System.out.println("Your age " + age + " is greater than or equal to 18, you can enter");
} 

In case you want to ensure age >= 18:

if (age >= 18) {
  System.out.println("Your age " + age + " is greater than or equal to 18, you can enter");
}

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

Upvotes: 1

Penguino
Penguino

Reputation: 2176

Your code will 'work' in the sense that it will print comments for each age input. But because you don't use else or nesting, it will always produce multiple comments. For example: if 17 is input it will trigger the comments for <>18, < 18, and <= 18. Also for the specific input of 18, one of the printed comments will conflict with the others, as both the = 18 and >= 18 comments will let you in, whereas the <= 18 comment will refuse you entry.

Upvotes: 0

user5434084
user5434084

Reputation: 149

If you need to use only 'if', its use is correct in your code but your conditions are repeating. Hint : If a person's age is supposedly 20, your code's second, third and fifth condition will be true and all of these will output. As you say in the question, you need to check if age is greater or less than 18, and also if you want to check if it is equal to 18, so from all of these if conditions, you only need two

Upvotes: 0

Dane Williams
Dane Williams

Reputation: 82

Your code is fine except that the last if statement should be just less than, not equal to or less than, and all of the if statements before the last two are extraneous so you can delete them. You only need to consider two possibilities: the person is 18+, or the person is younger than 18.

Upvotes: 0

Related Questions