+ 2
How get output hi?
public class Program { public static void main(String[] args) { int age = 16; int money = 400; if (age > 18) { if (money > 500) { System.out.println("Welcome!"); } else { System.out.println("hi"); } } } }
2 Answers
+ 2
Without using && operator how get answer hi just used if stament? Just like use in the program.
0
Both messages can only be printed in console, if the person is 19 or older, so you have to be sure the person is, as your variable only has an amount of 16 the condition will return false and the program will return after "if(age > 18){}" block. As the second if-clause (which is inside the first one and will only be executed if the first one is true) proves, if the user has a minimum amount of 501 "money", "hi" will only be printed, if the user is 19+ and has maximum 500 money.
With this it should work:
..
int age = 19;
int money = 500;
...
If you want your program to say hi, if the user is under 18 and has not enough money try the following:
public class Program { public static void main(String[] args)
{
int age = 16;
int money = 400;
if (age >= 18 && money >= 500) {
//&& connects the two conditions and the if-clause will only be true, if both conditions return true.
System.out.println("Welcome!");
} else {
//hi will be printed if one of the above conditions return false
System.out.println("hi"); }
}
}
}
Hope this answers your question :)