+ 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

29th Jun 2018, 2:11 PM
Abinash bhardwaj
Abinash bhardwaj - avatar
3 Réponses
+ 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"); } } }
29th Jun 2018, 2:38 PM
Arun Tomar
Arun Tomar - avatar
+ 1
Thanks
29th Jun 2018, 2:39 PM
Abinash bhardwaj
Abinash bhardwaj - avatar
0
you can also do as nested if:::can be written as::: if(y>0&&y<x)
29th Jun 2018, 4:21 PM
Nitish kumar jha
Nitish kumar jha - avatar