-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInput.java
More file actions
36 lines (26 loc) · 1.07 KB
/
UserInput.java
File metadata and controls
36 lines (26 loc) · 1.07 KB
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
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
// User input and input buffer
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
scanner.nextLine(); // To catch input buffer, we use scanner.nextLine()
System.out.print("Enter your gpa: ");
double gpa = scanner.nextDouble();
scanner.nextLine(); // To catch input buffer, we use scanner.nextLine()
System.out.print("Are you a student? (true/false): ");
boolean isStudent = scanner.nextBoolean();
System.out.println("Your name is " + name);
System.out.println("You are " + age + " years old");
System.out.println("Your gpa is : " + gpa);
if(isStudent) {
System.out.println("You are a student.");
} else {
System.out.println("You are NOT a student.");
}
scanner.close();
}
}