NEWS: Total Picture Views: 155283 | Total Article Views: 165619 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. PickUpThings with Becker Robot | 3. How to read a JSON and work with it | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

How to do simple If elseif and else Java

Description:

In this example I will show you how to use if elseif and else while reading the input from the keyboard.

Code:

    //Importing Scanner (Getting Keyboard Information)
    import java.util.Scanner;

    public class Main {


        public static void main(String[] args) {

            //Declaring Scanner
            Scanner input = new Scanner(System.in);

            int number = 0;

            //Print a line
            System.out.println("What is the number 1 in number?");
            //Looking for the integer entered
            number = input.nextInt();

            if(number == 1){
                System.out.println("Good job!");
            }else if(number == 0 || number == 2){
                System.out.println("You put 1 more or 1 less...");
            }else{
                System.out.println("Wrong answer.");
            }
        }
    }
Attachments: None

Tags: Scanner If elseif else Basic

Total Views: 939

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 09/01/14


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.