0

What lines to add to script to have user input for the int age = userinput?

for example for this code to set the age to my desired number using userinput public class Program { public static void main(String[] args) { int age = 25; if(age <= 0) { System.out.println("Error"); } else if(age <= 16) { System.out.println("Too Young"); } else if(age < 100) { System.out.println("Welcome!"); } else { System.out.println("Really?"); } } }

20th May 2017, 11:30 AM
virginijus kravcenko
virginijus kravcenko - avatar
5 Respostas
+ 19
It happens! don't worry... Now it works...enjoy ^_^
20th May 2017, 11:50 AM
Dev
Dev - avatar
+ 17
@virginijus It works! Try running it in the code playground... we have to import the Scanner class to get input from the user and then we create an object called "input" for our class. This object specifies the type of input as : input.nextInt(); Hope you got it :)
20th May 2017, 11:47 AM
Dev
Dev - avatar
+ 16
import java.util.*; // or import Java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); int age = input.nextInt(); if(age <= 0) { System.out.println("Error"); } else if(age <= 16) { System.out.println("Too Young"); } else if(age < 100) { System.out.println("Welcome!"); } else { System.out.println("Really?"); } } }
20th May 2017, 11:33 AM
Dev
Dev - avatar
+ 1
thx buddy,but i dont get it 😀,ive wrote same code as yours but it didnt worked for me
20th May 2017, 11:39 AM
virginijus kravcenko
virginijus kravcenko - avatar
+ 1
thanx again
20th May 2017, 11:51 AM
virginijus kravcenko
virginijus kravcenko - avatar