-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUserInput.java
39 lines (31 loc) · 1.45 KB
/
UserInput.java
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
28
29
30
31
32
33
34
35
36
37
38
39
//Importing Scanner class in Java file to use Scanner function.
import java.util.Scanner;
// OR you can use other method to import same file
//import java.util.*;
public class UserInput {
public static void main(String[] args) {
System.out.println("Taking Input from user");
// Creating Scanner class Object as "sc" in main function.
Scanner sc = new Scanner(System.in);
//Inputting Integer data type from user using sc object and nextInt() function
System.out.print("Enter 1 Integer: ");
int a = sc.nextInt();
System.out.print("Enter 2 Integer: ");
int b = sc.nextInt();
System.out.println("Sum of Two number is " + (a+b));
//Inputting Double data type from user using sc object and nextDouble() function
System.out.print("Enter 1 Double: ");
double aa = sc.nextDouble();
System.out.print("Enter 2 Double: ");
double bb = sc.nextDouble();
System.out.println("Sum of Two Double value is " + (aa+bb));
//Inputting String data type from user using sc object and next() function
System.out.print("Enter 1 String: ");
String str1 = sc.next();
System.out.println(str1);
//Inputting String data type from user using sc object and nextLine() function
System.out.print("Enter 2 String: ");
String str2 = sc.nextLine();
System.out.println(str2);
}
}