这是老师给我们的内容:
假设我们是一个为其用户提供公告板的在线服务。我们希望为用户提供过滤亵渎的选项。我们会认为猫,狗和美洲驼这两个词是亵渎的。编写一个程序,从键盘读取一个字符串,并测试该字符串是否包含我们的亵渎词。你的程序应该找到像cAt这样的单词,只有大小写才有区别。您还必须不识别仅包含可能被视为亵渎词的词。例如,Dogmatic连接是一个小类别,不应该被认为是亵渎。允许用户使用以下标点符号:(,。?"'()!:;)这意味着您将被要求找到“The”“Cat”不是一个孤独的骆驼。“或“猫,狗不能是美洲驼。”(注意:你只会对句子中第一次出现一个亵渎的词负责。但是,句子中可能包含一个以上的亵渎词。所以“连接猫“,不会发现一个亵渎的词,但是”狗狗,狗不是骆驼。“会回归2个亵渎的猫猫和骆驼)
所以,我尝试了这段代码:
import java.util.Scanner;
public class Degrees
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[]args)
{
System.out.println("Enter a sentence");
String sentence = keyboard.nextLine();
sentence = sentence.toLowerCase();
if(sentence.indexOf("cat ") != -1)
System.out.println("the profane word cat was detected");
else
System.out.println("the profane word cat wasn't detected");
if(sentence.indexOf("dog ") != -1)
System.out.println("the profane word dog was detected");
else
System.out.println("the profane word dog wasn't detected");
if(sentence.indexOf("llama ") != -1)
System.out.println("the profane word llama was detected");
else
System.out.println("the profane word llama wasn't detected");
}
}
然而,代码并没有发挥作用。如果我写了#34;教条狗"它应该只检查第一次出现的狗并看到它在一个单词中,然后忽略第二只狗。我的代码就是呃。我不知道我错过了什么以及我应该添加什么。我发誓,我已经连续6个小时了。请帮忙。我无法想到其他任何事情。所以,请接受我的建议和提示。
我也尝试过使用switch语句但由于某种原因它只执行了默认值。
答案 0 :(得分:1)
你必须创建一个“迷你解析器”,它将迭代句子中的单词,并检查它们中的每一个是否被认为是亵渎。
部分实施的解决方案:
public static void main(String[] args) {
String s = "The doggone cat, and dOg are not a llama.";
s = s.toLowerCase();
Scanner sc = new Scanner(s);
List<String> profaneWords = generateProfaneList();
int counter = 0;
while (sc.hasNext()) {
String word = sc.next();
for (String profane : profaneWords) {
if (word.matches(".*\\b" + profane + "\\b.*") && // check an exact match
! s.matches(".*" + profane + "[a-z].*\\b" + profane + "\\b.*") && // check that profane is not
! s.matches(".*[a-z]" + profane + ".*\\b" + profane + "\\b.*")) { // included as part of another word
counter++;
System.out.println("The word '" + profane + "' is profane!");
}
}
}
System.out.println(counter + " profane words were found");
}
private static List<String> generateProfaneList() {
List<String> profaneWords = new ArrayList<>();
profaneWords.add("dog");
profaneWords.add("cat");
profaneWords.add("llama");
return profaneWords;
}
<强>输出强>
The word 'cat' is profane!
The word 'llama' is profane!
2 profane words were found
答案 1 :(得分:1)
我建议使用此算法:
profaneWords
。wordsToAnalyze
profaneWords
中的每个字词(字符串),让我们调用当前字profane
:
profane
。我们称之为found
。使用值 no 初始化它。wordsToAnalyze
中的每个字词(字符串),让我们调用当前字analyzeMe
:
analyzeMe
。analyzeMe
是否等于 profane
。如果是,请将found
标记为是并中断for循环。analyzeMe
是否包含 profane
。如果是,则打破当前的循环。found
是,则报告亵渎性词语已被识别。我不会为上面的算法提供正确的Java实现。相反,只是一个伪代码(毕竟,它的功课,所以你的工作是代码,而不是我们的代码=)):
profaneWords = { "cat", "dog", "llama" } //why llama is profane? =(
wordsToAnalyze = sentence.split(" ") //this can be improved but you should not use regex yet
for each profane in profaneWords
begin for
found = false
for each analyzeMe in wordsToAnalyze
begin for
analyzeMe = trimNonCharacters(analyzeMe)
if (analyzeMe is equal to profane)
found = true
break
if (analyzeMe contains profane)
break
end for
if (found is true)
print "The word " + profane + " was found."
end for
对于trimNonCharacters
,您可以创建另一个方法,它基本上从字符串参数中读取每个字符,并删除其中的任何非字符并创建一个新字符串。您可以使用StringBuilder
:
public static String trimNonCharacters(String string) {
int startIndex = 0;
int endIndex = string.length();
for (int i = 0; i < string.length(); i++) {
if (Character.isLetter(string.charAt(i))) {
break;
}
startIndex++;
}
for (int i = string.length() - 1; i >= 0; i--) {
if (Character.isLetter(string.charAt(i))) {
break;
}
endIndex--;
}
String result = "";
if (startIndex <= endIndex) {
result = string.substring(startIndex, endIndex);
}
return result;
}
答案 2 :(得分:0)
这是正则表达式的理想选择:
System.out.println("Enter a sentence");
String sentence = keyboard.nextLine();
sentence = sentence.toLowerCase();
Pattern p = Pattern.compile("\Wcat\W|\Wdog\W|\Wllama\W");
Matcher m = p.matcher(sentence);
boolean matchFound = m.matches();
\ W将匹配任何非数字和非单词,因此示例连接不会触发匹配,但“cat would。
了解更多信息: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html