6

如果用户输入的字符串等于字符串数组中的任何字符串,我正在尝试设置布尔值 true。

我即兴创作并想出了这个

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = true;
for(String cancelWord : cancelWords) {
  if(!valueEqualsCancel) break;
  valueEqualsCancel =  valueEqualsCancel && value.equals(cancelWord);
}

valueEqualsCancel从来都不是真的。

有小费吗?

4

12 回答 12

23

您可以执行以下操作:

Arrays.asList(cancelWords).contains(value)

(见Arrays.asList()

如果您要进行这样的多个查询,则将所有单词放在 a 中Set并进行查询。

于 2013-08-04T15:31:13.867 回答
5

将 转换Array为 aList然后使用contains方法检查。

Arrays.asList(cancelWords).contains(value)

于 2013-08-04T15:21:49.980 回答
4

valueEqualsCancel永远不会是真的,因为当你找到cancelWord时你没有退出循环。

为了达到 break 语句,您需要 valueEqualsCancel 为 false。

例如,如果您在第一个循环之后搜索“取消”,则变量 valueEqualsCancel 为:

valueEqualsCancel =  valueEqualsCancel && value.equals(cancelWord) = TRUE && TRUE = TRUE;

所以在第二个循环中你不会中断。然后你再次评估表达式

valueEqualsCancel =  valueEqualsCancel && value.equals(cancelWord) = TRUE && FALSE = FALSE;      

因此在第三个循环中,您将退出并且 valueEqualsCancel 为假。

您可以通过这种方式更正您的代码:

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean found = false;
for(String cancelWord : cancelWords) {
  found = value.equals(cancelWord);
  if (found) break;
}
于 2013-08-04T15:23:12.913 回答
4

这是一个使用 Java 8 的 2-liner,易于阅读:

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = Arrays.stream(cancelWords).anyMatch(value::equals);
于 2018-03-09T14:00:26.047 回答
1

如果您有Apache Commons Lang,您可以使用StringUtils.equalsAny()

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = StringUtils.equalsAny(value, cancelWords);
于 2019-02-22T09:23:01.883 回答
0

使用此方法:

public boolean containsWord(String word, String[] words) {
   for(String cancelWord : words) {
      if(word.equals(cancelWord)) {
        return true;
      }
   }
   return false;

}

我希望这有帮助。

于 2013-08-04T15:34:07.637 回答
0

我想这应该会有所帮助:-

import java.util.Scanner;


public class check4 {
    public static void main(String args[])
    {
       Scanner scan = new Scanner(System.in);
       String value = scan.nextLine().toString();
       String[] cancelWords = {"cancel", "nevermind", "scratch that"};
       boolean valueEqualsCancel = false;
       for(int i=0;i<cancelWords.length;i++) {
          if(value.equals(cancelWords[i])) {
             valueEqualsCancel = true;
             break;
          }
       }
       System.out.println(valueEqualsCancel);
    }
}

输入:

camel

输出:

true
于 2013-08-04T15:36:44.170 回答
0

使用此方法

public static boolean  isContainsWords(String[] words,String Targent){

               boolean flag;
               for(String buf : words){
                     if(buf.equals(Targent))
                          return flag = true;
               }
               return flag = false;

}

看起来很完美

于 2013-08-04T15:48:19.027 回答
0
 foreach(string thisWord in cancelWords)
    if thisWord.equals(value)
       return true;

 return false;    // Fall-through
于 2013-08-04T15:24:12.887 回答
0

您的初始值已关闭,并且可以改进流程:

String[] cancelWords = {"cancel", "nevermind", "scratch that"};
boolean valueEqualsCancel = false; // fix
for(String cancelWord : cancelWords) {
  if(value.equals(cancelWord)) {
    valueEqualsCancel =  true;
    break;
  }
}
于 2013-08-04T15:28:15.457 回答
0

尝试

 String[] cancelWords = {"cancel", "nevermind", "scratch that"};     
  boolean valueEqualsCancel = true;     
  for(String cancelWord : cancelWords) {      
  if(!valueEqualsCancel) break;     
   valueEqualsCancel =  !(valueEqualsCancel && value.equals(cancelWord));     
 }
于 2013-08-04T15:42:57.337 回答
0

最适合您的情况的解决方案在一定程度上取决于取消字的数量以及比较是否区分大小写的必要性。我为两种可能的实现方式添加了代码:

// I would use a HashSet which needs constant time to compare, even with long lists of cancel words
// Note, that the use of toLowerCase() makes the comparison case insensitive.
@Test
public final void test2() {
    String value = "nevermind";
    HashSet<String> cancelWords = new HashSet<String>();
    cancelWords.addAll(Arrays.asList(new String[] {"cancel", "nevermind", "scratch that"}));

    boolean valueEqualsCancel = cancelWords.contains(value.toLowerCase());
    System.out.println("test2: " + valueEqualsCancel);
}

// You might like to know, which cancel word it was
@Test
public final void test3() {
    String value = "nevermind";
    String[] cancelWords = {"cancel", "nevermind", "scratch that"};
    Arrays.sort(cancelWords); // Prepare for binary search

    // The returned value indicates either which cancel word was used or, that nothing was found with result < 0.
    System.out.println("test3: nevermind " + Arrays.binarySearch(cancelWords, "nevermind"));
    System.out.println("test3: cancel " + Arrays.binarySearch(cancelWords, "cancel"));
    System.out.println("test3: something totally different " + Arrays.binarySearch(cancelWords, "something totally different"));
}
于 2013-08-04T16:13:14.033 回答