Saturday, February 2, 2013

Java, How to add two integer numbers , Scanner Class

Java, How to add two integer numbers , Scanner Class I use Scanner class, need to import Scanner class using import statement.

Then create a new scanner object called myscan, you could rename myscan.

Read two integer numbers from the console using the statement x= myscan.nextInt(), if you want to read double value u use nextDouble();

 The full code is as stated below;

//use scanner class

import java.util.Scanner;

// class name

public class AddNumber{
public static void main(String[] args){
// declare to variables;
int x, y;

// Initialize scanner class
Scanner myscan = new Scanner(System.in);

System.out.print("Enter 1st integer number");
x = myscan.nextInt();

System.out.print("Enter 2nd integer number");
y = myscan.nextInt();

System.out.println("Summation of x + y is : " + (x+y));

}

}

No comments:

Post a Comment