我正在尝试编写一个java程序来检测字符串中的特定单词,这些词被认为是“世俗的单词”。用户输入一个字符串,程序应该打印到屏幕上,显示被检测到的亵渎词,因为它应该打印没有检测到任何亵渎的词。对于这个节目,亵渎的词是“猫”、“狗”和“骆驼”。如果这些单词是大写的,程序应该检测它们。它不应将包括亵渎词在内的词语确定为亵渎词。例句:猫或紧张症不是亵渎的。
我已经写了一个程序,运行和检测亵渎的话相应。但是,它在屏幕上同时打印“检测到的亵渎词”和“没有检测到的亵渎词”。我很难弄清楚如何才能把它打印出来,当它们实际上是一个亵渎的词的时候,它们就被发现了。
import java.util.Scanner;
public class Ch3_Programming_Project3 {
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
boolean startProgram=true;
System.out.println("The words cat, dog, and llama are considered"
+" profane and will not be allowed.");
if (startProgram== true) {
System.out.println("Please enter a sentence:");
String sentence=keyboard.next();
sentence=sentence.toLowerCase();
boolean cat=true, dog=true, llama=true;
if (sentence.contains("cat")) {
cat=true;
}
if (sentence.contains("dog")) {
dog=true;
}
if (sentence.contains("llama")) {
llama=true;
} else {
System.out.println("No profanity detected." + " Sentence approved.");
}
if (cat==true || dog==true || llama==true) {
if (cat == true) {
if (cat==true&&dog==true&&llama==true) {
System.out.println("Profanity detected");
System.exit(0);
} else if (cat==true && dog==true) {
System.out.println("Profanity detected");
System.exit(0);
} else if (cat==true && llama==true) {
System.out.println("Profanity detected");
System.exit(0);
} else {
System.out.println("Profanity detected");
System.exit(0);
}
}
if (dog == true) {
if (dog==true && llama==true) {
System.out.println("Profanity detected");
System.exit(0);
} else {
System.out.println("Profanity detected");
System.exit(0);
}
}
if (llama == true) {
System.out.println("Profanity detected");
System.exit(0);
}
}
} else if (startProgram == false) {
System.exit(0);
} else {
System.out.println("Program was force closed.");
System.exit(0);
}
System.exit(0);
}//end main
}//end class我对java非常陌生,我真的很想学习。是的,这是作业。但我已经提交了它,并在这里试图了解我的错误在哪里,并学习。我试着搜索和搜索其他的帖子来找出我的问题,但是没有成功。
谢谢你能帮我的人。
发布于 2018-06-28 01:57:56
您的程序过于复杂,可以简化很多。
首先,可以将所有的if/else语句简化为一个语句。此外,正如可怕的沃巴特在评论中指出的那样,你应该使用keyboard.nextLine()从用户那里获得输入。
下面是Main方法的新代码:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("The words cat, dog, and llama are considered"
+ " profane and will not be allowed.");
System.out.println("Please enter a sentence:");
String sentence = keyboard.nextLine();
sentence = sentence.toLowerCase();
boolean foundProfane = false;
if (sentence.matches(".*\\bcat\\b.*")
|| sentence.matches(".*\\bdog\\b.*")
|| sentence.matches(".*\\bllama\\b.*")) {
foundProfane = true;
}
if (foundProfane) {
System.out.println("Profanity Detected!");
} else {
System.out.println("No profanity detected!");
}
}字符串中的.*\\b和\\b.*只允许捕获整个单词,因此在输入“目录”时不会对其进行标记,例如。
只要使用一个boolean来跟踪是否添加了一个粗俗的单词,您就可以将所有的支票保存在一个地方,如果找到一个顽皮的单词,只需翻转boolean即可。
另外,我删除了整个startProgram结构,因为它对程序没有任何影响。
注意:还有其他几个改进可以做,但由于您刚刚开始使用,这应该仍然符合您当前的技能水平。编码愉快!
发布于 2018-06-28 01:58:57
通过拆分句子并将坏单词存储在List中,可以大大简化代码。
List<String> pwords = Arrays.asList(new String []{"dog", "cat", "llama"});
String str = "it is raining a cat and a dog";
// or accept from keyBoard
//System.out.println("Please enter a sentence:");
// String str = keyboard.nextLine();
// str = str.toLowerCase();
System.out.println(str);
String [] arr = str.split("\\s+");
for (String w : arr) {
if (pwords.contains(w)) {
System.out.println("contains profanity");
return;
}
}
System.out.println("a clean sentence");编辑
输出
it is raining a cat and a dog
contains profanity
it is raining a cats and a dogs
a clean sentence发布于 2018-06-28 02:02:49
因此,您可以通过检查String是否包含亵渎词来启动程序,然后使用标志来指示是否包含任何亵渎词。不错的方法。
但是在这之后..。
如果这三个标志中的任何一个是真的:System.out.println("Profanity detected"); System.exit(0);
如果dog是真的:System.out.println("Profanity detected"); System.exit(0);
如果这两个标志中的任何一个是真的:System.out.println("Profanity detected"); System.exit(0);
其他:System.out.println("Profanity detected"); System.exit(0);
如果cat是真的:System.out.println("Profanity detected"); System.exit(0);
如果另外两个标志中的任何一个为真:System.out.println("Profanity detected"); System.exit(0);
我要到此为止,但我想你明白我的意思了。基本上,您的if-else站点中的每个分支都会导致相同的结果。你需要巩固它。您只需在您的第一个if-else分支中执行如下操作:
if(sentence.contains("llama") {
System.out.println("Contains profane word: llama");
}
//And so on这样,您甚至不需要标志或任何其他if-else(正如在其他答案中所指出的,您需要将keyboard.next()更改为keyboard.nextLine() )。
https://stackoverflow.com/questions/51073563
复制相似问题