In: Computer Science
Q1: Write a Java program that will display different messages depending on your age.
Your program should ask the user for his/her name and their age in years
and give one or more answers from the following ones below:
The answer to this question is as follows:
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String name=sc.nextLine();
int age=sc.nextInt();
if(age<25)//If age <25
{
System.out.println("You are not allowed to rent a car at the
moment");
if (age<18)//If age <18
{
System.out.println("You are not allowed to vote at the
moment");
}
if(age<16)//If age <16
{
System.out.println("You are not allowed to drive at the
moment");
}
}
else//otherwise
{
System.out.println("You can do anything that is legal");
}
}
}