Condition "else if" is used when the option in the condition you set is more than two. If there is only two option, you shouldn't be using "else if" anymore, just use "if-then-else". The program below shows an example on how to use "else if".
import java.util.*;
public class condition2{
public static void main(String[]args){
Scanner genioler = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = genioler.nextInt();
if (age < 18){
System.out.println("You are not allow to enter");
}
else if (age > 60){
System.out.println("You are not allow to enter");
}
else {
System.out.println("You are allow to enter");
}
}}
if (age < 18) - This is the first condition, if the user input a value below 18, then this program will run.
else if (age > 60) - This is the second option, above 60. If the age you enter is above 60, then this program will run.
else - If the age is not belong in the first two option, then this is the program that will run.
No comments:
Post a Comment