2016-01-28 287 views
-2

我需要找到另一种方式为这个(if else statement)替代,如果else语句


if(representatives.indexOf(word) == -1){ 
     representatives.add(word);  

    } 
    else if(repeatWords.indexOf(word) == -1){ 
     repeatWords.add(word); } 
+2

它的声音确定。有两种条件管理不同的列表。它们之间没有任何相似之处。 – iMBMT

+2

你可以使用'contains'而不是'indexOf',否则,这就好了。 –

+0

@bmt是否有比使用indexOf(word)更简单的方法? – rose

回答

1

与尝试含有作为DEVID解释。 contains是checkout对象包含与否的列表函数。所以你必须修改如下代码:

if(!representatives.contains(word)){ 
     representatives.add(word);  

    } 
    else if(!repeatWords.contains(word)){ 
     repeatWords.add(word); } 

它检查列表是否不包含对象,然后添加它。除此以外;一切似乎都好!