-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHowOldAreYou.java
More file actions
46 lines (37 loc) · 1.41 KB
/
HowOldAreYou.java
File metadata and controls
46 lines (37 loc) · 1.41 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
/*
http://programmingbydoing.com/a/how-old-are-you.html
How Old Are You?
Make a program which displays a different message depending on the age given. Here are the possible responses:
age is less than 16, say "You can't drive."
age is less than 18, say "You can't vote."
age is less than 25, say "You can't rent a car."
age is 25 or over, say "You can do anything that's legal."
Here's a sample run. Notice that a person who is under 16 will display three messages, one for being under 16, one for also being under 18, and one for also being under 25.
*/
import java.util.*;
class HowOldAreYou {
public static void printMessage(String name, int age){
if(age < 16){
System.out.println("You can't drive " + name + ".");
}
if(age < 18){
System.out.println("You can't vote " + name+ ".");
}
if(age < 25){
System.out.println("You can't rent a car " + name+ ".");
}
if(age >= 25){
System.out.println("You can do anything that's legal " + name+ ".");
}
}
public static void main(String[] args) {
String name;
int age;
Scanner keyboard = new Scanner(System.in);
System.out.print("Hey, what's your name?!? ");
name = keyboard.next();
System.out.print("Ok " + name + ", how old are you? ");
age = keyboard.nextInt();
printMessage(name, age);
}
}