-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInput.java
More file actions
27 lines (21 loc) · 1005 Bytes
/
UserInput.java
File metadata and controls
27 lines (21 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner;
/* This import statement gives me access to the class scanner
* This class has methods that give me access to standard input(input from the keyboard)
* */
class UserInput{
public static void main(String args[]){
//first lets create a new scanner object that will give us access to what the user will input
//System.in represents the standard input stream, which by default is the keyboard.
//It allows the program to read data entered by the user from the console.
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first Number: ");
int num1 = scanner.nextInt();
//nextInt() makes sure what the user enters is an integer
System.out.println("Enter the second Number: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("Sum of your two numbers is: " + sum);
scanner.close();
//have to close this once done because the scanner object got system resources so we have to release them once done
}
}