在这个程序中,“猫”,“狗”和“骆驼”被认为是亵渎,需要过滤掉一个句子。需要考虑CAPS(“cAT”),额外字母(“llama”)中的三个单词的任何版本。我的程序目前只能读取一个字,也不会读取大写/小写字母。我如何添加这个?Java中的我的亵渎过滤器有什么问题?
一个试验通过:
Enter a line to be checked for profanity:DoG CaT LlAmA |
Your input line
does not contain cat
does not contain dog
does not contain llama
This line would not be considered profane.
这是不对的,输入确实包含的话,被认为是亵渎。
我的代码:
import java.util.Scanner;
public class ProfaneFilter
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a line to be checked for profanity: ");
String sentence;
sentence = keyboard.next();
System.out.println(" ");
System.out.println("Your input line ");
sentence = keyboard.nextLine();
sentence.equalsIgnoreCase(sentence);
if (sentence.equals("cat") && sentence.equals("dog") && sentence.equals("llama"))
System.out.print("contains cat" + "\n" + "contains dog" + "\n" + "contains llama" + "\n" + "This line would be considered profane.");
else if (!sentence.equals("cat") && !sentence.equals("dog") && !sentence.equals("llama"));
System.out.println(" does not contain cat" + "\n" + " does not contain dog" + "\n" + " does not contain llama" + "\n" + "This line would not be considered profane.");
if (sentence.equals("cat"))
System.out.println("contains cat");
else if (sentence.equals("dog"))
System.out.println("contains dog");
else if (sentence.equals("llama"))
System.out.println("contains llama");
}
}
我知道我可能错过了一些重要的代码来获得我要找的输出,但我不知道它是什么。
您正在使用'String.equals'而不是'contains'。 'sentence.equalsIgnoreCase(句子);'什么都不做;你想要像'sentence = sentence.toLowerCase(句子);' –