Java 这是逻辑错误还是别的什么?
以下是老师分配给我们的内容: 假设我们是一个为其用户提供公告板的在线服务。我们想给我们的用户过滤掉亵渎的选项。我们会认为猫、狗和骆驼这个词是亵渎的。编写一个从键盘读取字符串的程序,并测试该字符串是否包含我们亵渎的单词。你的程序应该找到像cAt这样的词,它们只在大小写上有所不同。你也不能只识别那些可能被认为是亵渎的单词。例如,教条式的连接是一个小范畴,不应被视为亵渎。允许用户使用以下标点:(,.?“'()!:;)这意味着您可能会发现“猫”不是一只走狗骆驼。“猫,狗不能是骆驼。”(注意:您将只对一个句子中第一次出现的某个给定的亵渎词负责。但是,一个句子中可能包含多个亵渎词。因此,“连接猫”不会找到亵渎词,但“狗狗猫和狗不是骆驼”。将返回两个亵渎词cat和骆驼) 因此,我尝试了以下代码:Java 这是逻辑错误还是别的什么?,java,Java,以下是老师分配给我们的内容: 假设我们是一个为其用户提供公告板的在线服务。我们想给我们的用户过滤掉亵渎的选项。我们会认为猫、狗和骆驼这个词是亵渎的。编写一个从键盘读取字符串的程序,并测试该字符串是否包含我们亵渎的单词。你的程序应该找到像cAt这样的词,它们只在大小写上有所不同。你也不能只识别那些可能被认为是亵渎的单词。例如,教条式的连接是一个小范畴,不应被视为亵渎。允许用户使用以下标点:(,.?“'()!:;)这意味着您可能会发现“猫”不是一只走狗骆驼。“猫,狗不能是骆驼。”(注意:您将只对一个
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");
}
}
然而,代码并没有按应有的方式工作“它应该只检查第一次出现的狗,看它是否在一个词内,然后忽略第二只狗。我的代码只是ughh。我不知道我缺少什么和我应该补充什么。我已经连续6个小时在这方面我发誓。请帮助。我只是想不出任何其他东西。所以,请,我愿意接受建议和提示。”
我还尝试使用switch语句,但出于某种原因,它只执行默认值。这是一个很好的正则表达式候选者:
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将匹配任何非数字和非字,因此作为示例,CONTAINATE不会触发匹配,但“cat会”
有关更多信息:
你必须创建一个“迷你解析器”,它将迭代句子中的单词,并检查每一个单词是否被认为是亵渎的
部分实施的解决方案:
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;
}
我建议使用以下算法:
- 定义数组中的所有亵渎词语。我们称之为
亵渎词语
- 使用空格将句子分成几个字符串。这将存储到一个数组中,我们称之为
wordsToAnalyze
- 对于
中的每个单词(字符串),让我们调用当前单词profaneWords
:profane
- 创建一个标志来检查是否找到了
。我们将其称为亵渎
。使用值no初始化它已找到
- 对于
中的每个单词(字符串),让我们调用当前单词wordsToAnalyze
:analyzeMe
- 从
中修剪所有非字符analyzeMe
- 检查
是否等于analyzeMe
。如果等于,则将profane
标记为yes并中断for循环found
- 检查
是否包含analyzeMe
。如果包含,则断开循环电流亵渎内容
- 从
- 如果
为是,则报告已识别亵渎的单词found
- 创建一个标志来检查是否找到了
trimnocharacters
您可以创建另一个方法,该方法基本上读取字符串参数中的每个字符,并删除其中的任何非字符,然后创建一个新字符串。您可以使用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;
}
公共静态字符串修剪非字符(字符串){
int startIndex=0;
int endIndex=string.length();
对于(int i=0;i=0;i--){
if(Character.islet(string.charAt(i))){
打破
}
endIndex--;
}
字符串结果=”;
如果(startIndex),我认为问题在于您正在搜索“猫”“末尾有空格,以此类推。因此,如果单词在输入的末尾,它就找不到该单词,但如果我不添加空格,那么它将使串联成为亵渎。很好,但还不够好:它不会检测到仅在大小写上不同的亵渎单词!转换为小写首先消除了对更复杂正则表达式的需要。”问题“连接猫”中的“句子=句子。toLowerCase();”找不到一个亵渎的词。你的答案不适用于此。我真的没有学过正则表达式。因此,如果有其他方法解决问题,请分享。使用“\b(dog | cat | llama)\b”(每个术语之间的管道),并在匹配之前将您的句子转换为小写。学习正则表达式要比通过逐个字符分析模拟正则表达式好得多。首先检查堆栈溢出正则表达式常见问题解答:谢谢,这看起来好多了,但是我们不允许使用循环。此解决方案仍然错误:dog
检测到。@LuiggiMendoza那么我遗漏了一些东西-为什么不应该检测到dog
?它位于作业的底部(我的重点):但是,一个句子中可能包含多个亵渎的单词。因此,“连接猫”不会找到亵渎的单词,而是“狗去了的猫,狗不是骆驼”将返回两个亵渎的单词(猫和骆驼)@LuiggiMendoza我认为这是一个错误,因为它没有包含在需求列表中,进一步,请参阅OP对sanjab答案的评论。@alfasin是的,写得很快。修复了。在空格上拆分”“< /代码>不起作用,你应该考虑逗号、点等@ @ alasin点。因为它是伪代码,所以更容易修复。
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
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;
}