简体   繁体   English

java如何更改此代码,从查找所有真实对象查找为false

[英]java how to change this code from finding all true objects to find false

I have code that finds all the objects in the list that are true to getranonelectricity and prints them out. 我有代码可以找到列表中对getranonelectricity正确的所有对象并将其打印出来。 How do I change this code so it finds and prints out only false ranonelectricity objects? 我如何更改此代码,以便它仅查找并打印出错误的ranonelectricity对象?

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. 请阅读有关Java中的逻辑运算符的信息。 For example this: https://www.dummies.com/programming/java/logical-operators-in-java/ 例如: https : //www.dummies.com/programming/java/logical-operators-in-java/

You need a "Not" operator. 您需要一个“非”运​​算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM