To take input form the user In java we use a "Scanner" function which we import under a Java Package and the syntax for this Java Package is "import java.util.*;" then we create a scanner class in java. For better Understanding let us now see we use "Scanner" into a java program to take an input from the user.
so lets begin.
import java.util.Scanner;
public class userr {
public static void main(String[] args) {
int a,b,sum=0;
System.out.println("enter two integers");
Scanner kb = new Scanner(System.in);
a = kb.nextInt();
b = kb.nextInt();
sum=a+b;
System.out.println("sum of numbers is \t" +sum);
}
}
input a = 5 and b= 9 from the keyboard your program output will be :sum of numbers is 14
No comments:
Post a Comment