Taking Users Input to Add 2 no.s in Java- Using Scanner Class Method
Now, In this Java Program of Taking Users Input to Add 2 no.s in Java, you should have the knowledge of How Scanner Class Works also you should know following topics:
In Java, there are varieties of options available to accept users input & three most popular of them are:-
(a) Command Line Arguments.
(b) Using the Scanner Class.
(c) Using GUI Components.
Using Scanner Class
#Taking Users Input to Add 2 no.s in Java
public static void main(String args)
Scanner kb=new Scanner(System.in);
System.out.println("Enter 2 no.s:");
sum = num1 + num2;
System.out.println("Sum of "+num1 +" and " +num2 +" is: "+sum);
Enter 2 no.s:
Sum of 25 and 47 is: 72
In the Above method of Taking Users Input to Add 2 no.s in Java, we’re Importing a Scanner Class, Yes we do need to import Scanner class before using it
To do this, we use import keyword, Since Scanner class is inside the package called java.util,
that’s why we write import java.util.Scanner
Most Importantly as mentioned above Java uses all the mathematics operation therefore by Changing (+) operator, we can subtract (-), multiply (*), divide (/), floor divide (//) or find the remainder (%) of two numbers.
Ask your questions and clarify your/others doubts by commenting. Java Documentation