I have code that finds all the objects in the list that are true to getranonelectricity and prints them out. How do I change this code so it finds and prints out only false ranonelectricity objects?
Transport transports[]=new Transport[10];
transports[0]=Transport1;
transports[1]=Transport2;
transports[2]=Transport3;
transports[3]=Transport3;
transports[4]=Transport4;
transports[5]=Transport5;
transports[6]=Transport6;
transports[7]=Transport7;
transports[8]=Transport8;
transports[9]=Transport9;
for (Transport transportssssss : transports) {
if (transportssssss instanceof car) {
if (((car) transportssssss).getranonelectricity()) {
System.out.println(transportssssss);
Change
if(((car) transportssssss).getranonelectricity()){
to
if(!((car) transportssssss).getranonelectricity()){
The !
operator will invert the truth value of any Boolean expression.
Please read about Logical Operators in Java. For example this: https://www.dummies.com/programming/java/logical-operators-in-java/
You need a "Not" operator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.