+ 1
How I can write a code getting the age of the user then if the age is > 18 print welcome else print too young ?
I am a beginner learning java and I need to practice on how to use Scanner , variable and if statement . I know how to code all of them but Idk how to connect the data I get from the user by "Scanner" with if statement or with the variable then if statement
5 Réponses
+ 4
Simpley store you data taken by user in veriable and check if user is young or not
Ex storing value in veriable
Scanner obj = new Scanner(System.in);
int age = obj.nextInt();
+ 1
//In java course (Conditional statement) This code is demonstrated
https://www.sololearn.com/learn/Java/2143/
0
For Scanner : import class Scanner by
import java.util.Scanner ;
Make Scanner object by
Scanner in=new Scanner(System.in);
Now you have varying methods for taking input for specific inputs..
if(condition)
{
...
}
Ex:
int age=18;
if(age>18)
System.out.println("Adult");
else
System.out.println("young") ;
For all these you can find better explanation from tutorial link:
https://www.sololearn.com/learn/Java/2220/
https://www.sololearn.com/learn/Java/2139/
https://www.sololearn.com/learn/Java/2143/
0
I know how to code all of them but Idk how to connect the data I get from the user by "Scanner" with if statement or with the variable then if statement
0
Scanner in=new Scanner(System.in);
//To Store an int
int age=in.nextInt();
//Store a long
long age=in.nextLong();
//Store a float
float =in.nextFloat();
//Store a double
double age=in.nextDouble();
//Store a boolean -
boolean b=in.nextBoolean();
//Store a complete line -
String s=in.nextLine();
//Read a word -
String word=in.next();
//Store a byte -
byte a=in.nextByte();
Or if(in.nextInt() > 18) {....}