The if…else statement in Java program

Post on January 6th,2012 by |Posted in programming languages | Tagged in , , | No Comment | Send Email

We will continue with our coding tutorial today using the previous class but this time we are going to use the if…else statement to print out the different screen output based on certain condition we set. The code is as follow:


import java.util.Scanner;

public class PlusNumber {

  public static void main(String[] args) {

    int firstNumber;
    int secondNumber;
    int total;

    Scanner input = new Scanner(System.in);

    System.out.print("Enter first integer : ");

    firstNumber = input.nextInt();

    System.out.print("Enter second integer : ");

    secondNumber = input.nextInt();

    total = firstNumber + secondNumber;

    if(total < 13) {
      
      System.out.println("The total is : "
        + total + " which is less than 13!");
  
    } else {

      System.out.println("The total is : " + total);

    }

   }

}

Run it and you will get below outcome:

Next Page :

Javascript replace method review

Javascript replace method review

How to use Java Scanner Class

This article will show you how to use Java Scanner Class with simple Java code.

Show text on JFrame with Java program

Show text on JFrame with Java program!

Java Scanner Class

Java Scanner class tutorial will show you how to create an instance of the scanner class and read in the input from the user then print out the sum of those inputs.

Creating and Using Interfaces in Java Program

Creating and Using Interfaces in Java Program will show you how to write simple java program using Interfaces.

Create a simple Java method

An article about how to create a simple Java method.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>