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?
5 Respuestas
+ 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);
}
}
+ 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.
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
}
}
}
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.
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?