Make a program which displays a different message depending on the age given. Here are the possible responses:
Hey, what's your name? Harry
Ok, Harry, how old are you? 17
You can't vote, Harry.
You can't rent a car, Harry.
- 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."
Hey, what's your name? Harry
Ok, Harry, how old are you? 17
You can't vote, Harry.
You can't rent a car, Harry.
Solution:
//Importing the Scanner Class import java.util.Scanner; public class Question4 { public static void main(String args[]){ //Creating Scanner object Scanner input = new Scanner(System.in); //Prompting user for input System.out.print("Hey, What's your name? "); //Reading name String name=input.next(); //Displaying the welcome message System.out.println("Ok, "+name+", How old are you? "); //Reading age int age=input.nextInt(); if(age<0) System.out.println("Invalid age!!"); else{ 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+"."); else System.out.println("You can do anything, that's legal."); } } }
No comments:
Post a Comment