Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查列表是否正确_Java_List_Collections_Boolean - Fatal编程技术网

Java 检查列表是否正确

Java 检查列表是否正确,java,list,collections,boolean,Java,List,Collections,Boolean,我对编程还不熟悉,并尝试创建一个方法,该方法迭代列表并返回true或false,以检查比萨饼是否适合素食者。只有当列表中的所有配料都适合素食者时,这才是真的 以下是我的方法: public boolean vegStatus(){ boolean veg1 = false; for(PizzaTopping topping : topList){ if( (topping.isVeg() == true )) { veg1 = true

我对编程还不熟悉,并尝试创建一个方法,该方法迭代列表并返回true或false,以检查比萨饼是否适合素食者。只有当列表中的所有配料都适合素食者时,这才是真的

以下是我的方法:

public boolean vegStatus(){

    boolean veg1 = false;

    for(PizzaTopping topping : topList){
        if( (topping.isVeg() == true )) {
            veg1 = true;
        }
        else if(topping.isVeg() == false) {
            veg1 = false;
        }   
    }
    return veg1;
}

这种方法不起作用,它产生了错误的答案。我如何更改它,使其查看列表中的所有配料,并且仅当所有配料都适合素食者时才返回真值?

请记住,当配料不是素食者时,您不需要关心其他配料,此后没有任何东西可以改变比萨饼的状态。因此,没有必要检查其余部分。返回只是从所有循环中断开。如果比萨饼没有被素食者不喜欢的东西污染,你知道它一定是素食者

public boolean vegStatus(){

    for(PizzaTopping topping : topList){
        if( !topping.isVeg() ) {
            return false;
        }

    }
    return true;
}

只要记住,当一个配料不是素食者时,你不需要关心其他的配料,在那之后没有什么可以改变比萨饼的状态。因此,没有必要检查其余部分。返回只是从所有循环中断开。如果比萨饼没有被素食者不喜欢的东西污染,你知道它一定是素食者

public boolean vegStatus(){

    for(PizzaTopping topping : topList){
        if( !topping.isVeg() ) {
            return false;
        }

    }
    return true;
}

只要记住,当一个配料不是素食者时,你不需要关心其他的配料,在那之后没有什么可以改变比萨饼的状态。因此,没有必要检查其余部分。返回只是从所有循环中断开。如果比萨饼没有被素食者不喜欢的东西污染,你知道它一定是素食者

public boolean vegStatus(){

    for(PizzaTopping topping : topList){
        if( !topping.isVeg() ) {
            return false;
        }

    }
    return true;
}

只要记住,当一个配料不是素食者时,你不需要关心其他的配料,在那之后没有什么可以改变比萨饼的状态。因此,没有必要检查其余部分。返回只是从所有循环中断开。如果比萨饼没有被素食者不喜欢的东西污染,你知道它一定是素食者

public boolean vegStatus(){

    for(PizzaTopping topping : topList){
        if( !topping.isVeg() ) {
            return false;
        }

    }
    return true;
}

您应该将
veg1
变量初始化为true,并且只有在发现不适合素食者的配料时才在循环中更改它

public boolean vegStatus(){

    boolean veg1 = true;

    for(PizzaTopping topping : topList){
        if(topping.isVeg() == false) {
            veg1 = false;
        }   
    }
    return veg1;
}

您应该将
veg1
变量初始化为true,并且只有在发现不适合素食者的配料时才在循环中更改它

public boolean vegStatus(){

    boolean veg1 = true;

    for(PizzaTopping topping : topList){
        if(topping.isVeg() == false) {
            veg1 = false;
        }   
    }
    return veg1;
}

您应该将
veg1
变量初始化为true,并且只有在发现不适合素食者的配料时才在循环中更改它

public boolean vegStatus(){

    boolean veg1 = true;

    for(PizzaTopping topping : topList){
        if(topping.isVeg() == false) {
            veg1 = false;
        }   
    }
    return veg1;
}

您应该将
veg1
变量初始化为true,并且只有在发现不适合素食者的配料时才在循环中更改它

public boolean vegStatus(){

    boolean veg1 = true;

    for(PizzaTopping topping : topList){
        if(topping.isVeg() == false) {
            veg1 = false;
        }   
    }
    return veg1;
}

以下是一个清晰的逻辑:

public boolean vegStatus(){
    for(PizzaTopping topping : topList) {
        if(!topping.isVeg())
            return false;
    }

    return true;
}

它会检查所有的
配料
,如果其中任何一种不是素食,它会立即返回
false
,而不检查其他配料(不必,对吧?)。如果它检查了所有这些,但没有一个不是素食,则返回
true

以下是一个干净的逻辑:

public boolean vegStatus(){
    for(PizzaTopping topping : topList) {
        if(!topping.isVeg())
            return false;
    }

    return true;
}

它会检查所有的
配料
,如果其中任何一种不是素食,它会立即返回
false
,而不检查其他配料(不必,对吧?)。如果它检查了所有这些,但没有一个不是素食,则返回
true

以下是一个干净的逻辑:

public boolean vegStatus(){
    for(PizzaTopping topping : topList) {
        if(!topping.isVeg())
            return false;
    }

    return true;
}

它会检查所有的
配料
,如果其中任何一种不是素食,它会立即返回
false
,而不检查其他配料(不必,对吧?)。如果它检查了所有这些,但没有一个不是素食,则返回
true

以下是一个干净的逻辑:

public boolean vegStatus(){
    for(PizzaTopping topping : topList) {
        if(!topping.isVeg())
            return false;
    }

    return true;
}
它会检查所有的
配料
,如果其中任何一种不是素食,它会立即返回
false
,而不检查其他配料(不必,对吧?)。如果它检查了所有这些,但没有一个不是素食者,它会返回
true

这样做

public boolean vegstatus(){    
    for(PizzaTopping topping : topList){
        if( !topping.isVeg()) {
            return  false; //Failed so anymore he is not a vegetarian 
        }       
    }
    return true;// Never failed above condition so he should be vegetarian. 
}
你喜欢这样吗

public boolean vegstatus(){    
    for(PizzaTopping topping : topList){
        if( !topping.isVeg()) {
            return  false; //Failed so anymore he is not a vegetarian 
        }       
    }
    return true;// Never failed above condition so he should be vegetarian. 
}
你喜欢这样吗

public boolean vegstatus(){    
    for(PizzaTopping topping : topList){
        if( !topping.isVeg()) {
            return  false; //Failed so anymore he is not a vegetarian 
        }       
    }
    return true;// Never failed above condition so he should be vegetarian. 
}
你喜欢这样吗

public boolean vegstatus(){    
    for(PizzaTopping topping : topList){
        if( !topping.isVeg()) {
            return  false; //Failed so anymore he is not a vegetarian 
        }       
    }
    return true;// Never failed above condition so he should be vegetarian. 
}

你应该这样做

public boolean vegStatus(){

boolean veg1 = true;   // by default its true

for(PizzaTopping topping : topList){
    if(topping.isVeg() == false) {
        veg1 = false; // if any one id  not suitable then its false
    }   
}
return veg1; // at the end returning the result.. 
}

如果您有任何疑问,那么您可以再次询问

你应该这样做

public boolean vegStatus(){

boolean veg1 = true;   // by default its true

for(PizzaTopping topping : topList){
    if(topping.isVeg() == false) {
        veg1 = false; // if any one id  not suitable then its false
    }   
}
return veg1; // at the end returning the result.. 
}

如果您有任何疑问,那么您可以再次询问

你应该这样做

public boolean vegStatus(){

boolean veg1 = true;   // by default its true

for(PizzaTopping topping : topList){
    if(topping.isVeg() == false) {
        veg1 = false; // if any one id  not suitable then its false
    }   
}
return veg1; // at the end returning the result.. 
}

如果您有任何疑问,那么您可以再次询问

你应该这样做

public boolean vegStatus(){

boolean veg1 = true;   // by default its true

for(PizzaTopping topping : topList){
    if(topping.isVeg() == false) {
        veg1 = false; // if any one id  not suitable then its false
    }   
}
return veg1; // at the end returning the result.. 
}

如果您有任何疑问,那么您可以再次询问

当你发现一个不是素食的配料时,返回false。当你发现一个不是素食的配料时,返回false。当你发现一个不是素食的配料时,返回false。当你发现一个不是素食的配料时,返回false。无需检查true是否为true:-)无需检查true是否为true:-)无需要检查true是否为true:-)无需检查true是否为true:-)