+ 1
Why isn't my code saying welcome?
5 Antworten
+ 2
your code is
int age=42;
// so remember age is 42
if(age>=17){
//here age is greater than 17 so this will be executed.
if(age<16){
//now here lies the problem age is not smaller than 16. it's 26 more than 16.
hope you understand 42<16 is false. so this will not be executed.
System.out.print("welcome");
//this will not be printed. as it was false. so the else of it will be executed
else....
Solution :
your program is wrong . welcome will only be printed if a number is greater than or equal to 17 as well as less than 16 . which is impossible both can't happen at the same time so. change the sign of < to >.
see this code .
execute this code . this is what you might have meaned. use "<" instead of >. as any number greater than or equal to 17 can never be smaller than 16.
// Created by Whitehat
public class Program
{
public static void main(String[] args) {
int age=42;
if (age>=17)
//instead of age<16 which is wrong
if (age>16) {
System.out.println("Welcome"); }
else { System.out.println("Bye you are too young"); }
else { System.out.println("Really"); }
}
}
+ 1
no offense but i have done it before when age=80 so that is not the reason it isn't working
+ 1
ohhh ok i thought that the else statement was going to run if the second if statement rang true
+ 1
public class Program
{
public static void main(String[] args)
{
int age=42;
if (age>=17)
{ System.out.println("Welcome");
}
else if (age<16)
{
System.out.println("Bye you are too young");
}
else { System.out.println("Really");
}
}
}
use this code
0
inzinm HKipob ge je