-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgeCheck.java
More file actions
47 lines (38 loc) · 1.44 KB
/
Copy pathAgeCheck.java
File metadata and controls
47 lines (38 loc) · 1.44 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
/*The following code has many problems. First, it has a few syntax errors; find and fix them.
Second, it has a logic problem: for some value(s), it prints the wrong answer. Find any logic problems and fix them.
Lastly, the program uses if and else in a clumsy way. Improve the style of the code to be more elegant and avoid redundancy.
public class AgeCheck {
public static void main(String[] args) {
System.out.print("Your age? ");
Scanner console = new Scanner(System.in);
int myAge = console.nextInt();
message(myAge);
}
// Displays message about driving to user based on given age
public static void message(int age) {
if (myAge >= 16) {
System.out.println("I'm old enough to drive!");
}
if (myAge <= 16) {
System.out.println("Not old enough yet... :*(");
}
}
}
*/
import java.util.Scanner;
public class AgeCheck {
public static void main(String[] args) {
System.out.print("Your age? ");
Scanner console = new Scanner(System.in);
int myAge = console.nextInt();
message(myAge);
}
// Displays message about driving to user based on given age
public static void message(int age) {
if (age >= 16) {
System.out.println("I'm old enough to drive!");
} else {
System.out.println("Not old enough yet... :*(");
}
}
}