CHALLENGE????? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

CHALLENGE?????

How do you create a program that asks the user to input their age, and if they enter a age below 0, the progam displays message "dont be silly", and asks the same question unril thu enter a valid age. any takers?

5th Dec 2016, 12:55 AM
Winiata Hunia
Winiata Hunia - avatar
5 Answers
+ 7
Here it is import java.util.*; public class Program { public static void main(String[] args) { int age; System.out.println("Please enter your age"); Scanner sc = new Scanner(System.in); age=sc.nextInt(); while(age==0) { if(age==0) System.out.println("Don\'t be Silly"); age=sc.nextInt(); } System.out.println("You have entered\n Your age is "+age); } }
5th Dec 2016, 1:07 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 4
It not showing because playground is online compiler. It read your code like normal text and if there is scanner or buffer reader is used it ask user input at one time and then send your input and code to server compiler. It compile the code and then send you raw text data. Please, use an offline compiler. Download aide it is offline java compiler for android You will see that my code working.
5th Dec 2016, 4:33 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
import java.util.Scanner; public class Program{ private static Scanner sc; //scanner so user can input into dialog box public static void main(String[] args) { Scanner sc = new Scanner(System.in); int age; //user can enter age age = sc.nextInt(); if(age <= 0) { System.out.println("Dont be silly"); //if user enters invalid age user will see this message } else if(age <= 40) { System.out.println("Hi young thing"); } else if(age > 40) { System.out.println("Wow you are old!"); } else { System.out.println("Really?"); //I need to figure out how to add and loop a question until correct answe is entered } } }
5th Dec 2016, 4:24 AM
Winiata Hunia
Winiata Hunia - avatar
0
thanx Aditya I ran your code thru code playground and i was able to enter a age of -0 meaning i dont think it worked.
5th Dec 2016, 4:25 AM
Winiata Hunia
Winiata Hunia - avatar
0
i worked on the bive code, but csnt figure how to add the "loop" that makes the program ask user to input age again. any ideas?
5th Dec 2016, 4:26 AM
Winiata Hunia
Winiata Hunia - avatar