2024_Programming_Training_D.../CS4 - Challenge Solution/Main.java

43 lines
1.8 KiB
Java

import java.util.Scanner;
/*
CS4 Challenge Solution
Original Challenge Description: Built a program that requests the user's age,
and return on of the following:
<= 18 - You're a youngin'
> 18 AND <= 35 - The early adult years
> 35 AND <= 55 - Middle aged, time to buy a really expensive car
> 55 AND <= 64 - Getting old...
> 64 - Retirement! Time to live out the golden years.
*/
public class Main {
public static void main(String[] args) {
//Start with setting up System.in through a Scanner
Scanner scan = new Scanner(System.in);
//Request the user's age, as an int
System.out.print("Enter your age: ");
int age = scan.nextInt();
//Close the scanner, because good practice
scan.close();
//This is a larger if statement than what we saw in the demos and for good reason.
//We cover all the different case scenarios here as described in the challenge, but
//notice how I didn't explicitly put an else if statement for > 64 years old condition.
//Because it's the last condition, and we have if's or else if's for the other possible
//conditions, we can put the last possible condition as the "else" statement.
if(age <= 18) { // <= 18
System.out.println("You're a youngin'");
} else if(age > 18 && age <= 35) { // > 18 AND <= 35
System.out.println("The early adult years");
} else if(age > 35 && age <= 55) { // > 35 AND <= 55
System.out.println("Middle aged, time to buy a really expensive car");
} else if(age > 55 && age <= 64) { // > 55 AND <= 64
System.out.println("Getting old...");
} else { // > 64
System.out.println("Retirement! Time to live out the golden years.");
}
}
}