java - Don't know how to display only one syso if i have more "if" who are satisfied -
how make simple program show me 1 answer, if multiple conditions satisfied like: if age<16 syso("you can't rent cars") , if age <18 syso("you can't vote").
for example, if introduce 17 want display ("you can't vote") not ("you can't vote" , "you can't rent cars").
i tried use 2 conditions sticked "&&", didn't work.
code comments
import java.util.scanner; public class assign1 { public static void main(string[] args) { scanner x = new scanner(system.in); int age; system.out.println("insert age "); age = x.nextint(); if (age<16) { system.out.println("you can't drive"); } if (age<18 && age<16) { system.out.println("you can't vote"); } if (age<25) { system.out.println("you can't rent cars"); } if (age>25) { system.out.println("you can "); } } }
this want:
public class test { public static void main(string[] args){ int age = 17; if(age < 16){ system.out.println("you can't drive"); } else if(age < 18){ system.out.println("you can't vote"); } else if(age < 25){ system.out.println("you can't rent cars"); } else{ system.out.println("you can anything"); } } }
output:
you can't vote
the program print first if satisfied.
but suggest study conditional operators , logic. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
Comments
Post a Comment