2016-04-26 71 views
-1

在这个程序中,“猫”,“狗”和“骆驼”被认为是亵渎,需要过滤掉一个句子。需要考虑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"); 
    } 
} 

我知道我可能错过了一些重要的代码来获得我要找的输出,但我不知道它是什么。

+0

您正在使用'String.equals'而不是'contains'。 'sentence.equalsIgnoreCase(句子);'什么都不做;你想要像'sentence = sentence.toLowerCase(句子);' –

回答

0

您可以使用contains而不是equals来获得所需的输出,因为equals查找完全匹配。此外,如果您可以将输入字符串转换为小写,它可能会有所帮助。

请尝试运行下面的代码,看看它是否工作。如果您觉得它更具可读性,也可以随时更换开关盒。

package details; 

import java.util.Scanner; 

import org.omg.Messaging.SyncScopeHelper; 

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().toLowerCase(); 

StringBuffer stringBuffer = new StringBuffer(); 
if(sentence.contains("cat")){ 
    stringBuffer.append("The sentence contains cat \n"); 
}else{ 
    stringBuffer.append("The sentence doesnt contains cat \n"); 
} 
if(sentence.contains("dog")){ 
    stringBuffer.append("The sentence contains dog \n"); 
}else{ 
    stringBuffer.append("The sentence doesnt contains dog \n"); 
} 
if(sentence.contains("llama")){ 
    stringBuffer.append("The sentence contains llama \n"); 
}else{ 
    stringBuffer.append("The sentence doesnt contains llama \n"); 
} 
System.out.println("stringBuffer is "+stringBuffer.toString()); 

}

}

+0

嘿,感谢您的输入!虽然这并不是我所需要的,但它确实帮助我理解了解决这个问题的新方法!提示:我仍然是初学者:P。 – Matt

0

这是我会怎么写。这将使用一系列你的亵渎词,并遍历它们以查看句子是否包含它们。

import java.util.Scanner; 

public class PF { 
    public PF() { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Enter a line to be checked for profanity: "); 
     String sentence = keyboard.nextLine(); 
     System.out.println("\nYour input line: " + sentence); 

     String [] prof = new String []{"cat", "dog", "llama"}; 
     for (String s : prof) 
     { 
      if (sentence.toLowerCase().contains(s)) 
       System.out.println("The sentence contains " + s); 
      else 
       System.out.println("The sentence does not contain " + s); 
     }  
    } 
    public static void main(String...banana) { new PF();} 
} 
+0

嘿,感谢您的输入!虽然这并不是我所需要的,但它确实帮助我理解了解决这个问题的新方法! – Matt