+ 1

Why i am not getting the last statement in output “offer anything on the menu”

package Mamun; public class Task1 {     public static void main(String[] args) {         //    Vegan Menu         //   Are both vegan? only offer up vegan dishes.         //   At least one vegan? make sure offer up some vegan options.         //   Else, offer anything on the menu         boolean isGuestOneVegan = true;         boolean isGuestwoVegan = true;         boolean onlyOfferVeganDish = isGuestOneVegan && isGuestwoVegan;         System.out.println("Both vegan: Only offer vegan dish");         boolean oneGuestisVegan = isGuestOneVegan || isGuestwoVegan;         if (oneGuestisVegan) {             System.out.println("At least one vegan: offer some vegan dish");}             else {             System.out.println("Offer anything on the menu");}     } }

9th Dec 2018, 5:14 AM
Mamun Rahman
Mamun Rahman - avatar
4 Answers
+ 14
Both if and else block cannot be executed together. The logic is, if at least one guest is vegan, then vegan dish will be offered and if both are non-vegan, then the last statement will be printed. According to your initial assignment, both guests are vegan, and thus oneGuestIsVegan is true. If you assign both the variables as false, then the else block will work (not the if block). This is how if-else logic works. If the condition is true -> if block will be executed If the condition is false -> else block will be executed
9th Dec 2018, 5:49 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 8
Because oneGuestisVegan is true, so the else block won't be executed.
9th Dec 2018, 5:40 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 1
Thank you Shamima
9th Dec 2018, 9:38 AM
Mamun Rahman
Mamun Rahman - avatar
0
how should i do it that i get the else statement in output as well
9th Dec 2018, 5:42 AM
Mamun Rahman
Mamun Rahman - avatar