Repository navigation
Expand file tree
/
Copy pathLesson3_userInput.java
More file actions
69 lines (48 loc) · 1.89 KB
/
Copy pathLesson3_userInput.java
File metadata and controls
69 lines (48 loc) · 1.89 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;
public class Lesson3_userInput {
public static void main(String[] args) {
// Calculate area of a rectangle
double width = 0;
double height = 0;
double area = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the width: ");
width = sc.nextDouble();
System.out.print("Enter the height: ");
height = sc.nextDouble();
area = width * height;
System.out.println("The area is: " + area + "cm");
sc.close();
/*
// COMMON ISSUES
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt(); // Leaves a newline token in the buffer
sc.nextLine(); // Gets rid of newline token
System.out.println("Enter your favorite color");
String color = sc.nextLine();
System.out.println("You are " + age + " years old");
System.out.println("You like the color " + color);
*/
/*
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = Integer.parseInt(sc.nextLine());
System.out.print("What is your GPA?: ");
double gpa = Double.parseDouble(sc.nextLine());
System.out.println("Are you a student? (true/false): ");
boolean isStudent = Boolean.parseBoolean(sc.nextLine());
System.out.println("Hello " + name);
System.out.println("You are " + age + " years old!");
System.out.println("Your GPA is: " + gpa);
if(isStudent) {
System.out.println("You are enrolled as a student");
}
else {
System.out.println("You are NOT enrolled");
}
*/
sc.close(); // when something is opened, typically, it should be closed once finished
}
}