Most of time we need to take input from users. why we need to take input from user. is it important ? Yes , it is important because I will explain by one example . let me allow,
we will write one simple program adding a two numbers. Two numbers are input for code. please refer below code snippet. we did hard code like a = 10 and b = 20,
suddenly we need to change means input value. Again go to code and change the variable value. it seems easy for one or two times. n times need to change. it is very difficult to do. So we will take input from user . user can give any input . code should be work and it should give correct output. That sounds good. so we will use scanner class .
Code bit : Add two number in Java
Scanner class will help us to take input from user. it is present in Java. util package.
Let's see code
In the code, we will take two integer number by user Using Scanner
Steps :
- Create scanner object
- Take first number from user (Console) using scanner
- Take second number from user (Console)
- Additional logic
- Display the result
- Scanner Object - it is used for taking user input.
- we need to import scanner from java.util.Scanner;
- Creating scanner object like Scanner sc = new Scanner(System.in);
- nextInt() - this method is used for taking int type values from user
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments