Wednesday 5 March 2014

JAVA IF CONDITION

The following program demonstrates how java If...else statement works. The program imports java.util.Scanner package, which will handle the user input by scanning the inserted variable. First, the program ask for the year Nigeria got it's independence, if the user guesses correctly by putting '1960', the program will print "correct answer, you are genius", otherwise, the program will print "wrong answer, please try again." Also if the user luckily insert '1959', the program will print "You are almost correct, keep trying.". I just write the program to demonstrate how java if...else condition statement is used.

import java.util.Scanner;
public class BRRead{
public static void main(String args[]){
int a;
Scanner in = new Scanner(System.in);

System.out.println("Enter The year Nigeria got it's independence ");
    a = in.nextInt();
if (a==1960)
{
    System.out.println("Correct answer, you are genius!!!");
}
else if(a==1959)
{
    System.out.println("You are almost correct, keep trying.");
}
else
{
    System.out.println("Wrong answer, please try again.");
}
}
}

if  you have any java IDE such as Netbeans, Jgrasp or Eclipse, just copy the above program and paste it into your IDE and save the file as BRRead and then compile it, remember java is case-sensitive.

2 comments: