+ 1
What's wrong here?
import java.util.Scanner; class myclass { public static void main(String args[]) { Scanner Sc = new Scanner(System.in); int x = 18; if (Sc.nextInt() > 0) { if (Sc.nextInt() < x) { System.out.println("Too Young");} else { System.out.println("Welcome"); } } else { System.out.println("Error"); } } } It works if the user input is 0. But for any other int value, it doesn't work
3 odpowiedzi
+ 4
Abinash bhardwaj
this will help you
asked your self in this program you need two input from the user.
if not
-->>your problems is that you taking two input
..
Scanner is used to take input from user
Sc.nextInt();
when you write these statement.
it take input from the user..
so what i do
i asked input from the user at once time.
and store in a variables y
import java.util.Scanner;
class myclass
{
public static void main(String args[])
{
Scanner Sc = new Scanner(System.in);
int x = 18;
int y=Sc.nextInt();
if (y> 0) {
if (y< x) {
System.out.println("Too Young");}
else {
System.out.println("Welcome");
}
}
else {
System.out.println("Error");
}
}
}
+ 1
Thanks
0
you can also do as
nested if:::can be written as:::
if(y>0&&y<x)