This is a simple program to demonstrate the use of Scanner class for taking user input
{
{
int num1, num2, num3, sum;
System.out.print(" Enter 1st Number: ");
num1 = input.nextInt();
num2 = input.nextInt();
System.out.print(" Enter 3rd Number: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
System.out.println("sum= " + sum);
}
}
Scanner is a class available in java.util package. The Scanner class is imported to our program using the import statement. This is a simple program to enter three numbers from the console and the sum of the numbers will be displayed. Firstly Scanner class object is created using the statement ‘Scanner input = new Scanner(System.in);’. Constructor of Scanner class takes ‘System.in’ as argument. ‘nextInt ()’ function of Scanner class is used to retrieve values.
import java.util.Scanner;
public class Inp
public static void main(String args[])
Scanner input = new Scanner(System.in);
System.out.print(" Enter 2nd Number: ");
No comments:
Post a Comment