ฉันติดอยู่กับภารกิจ! กรุณาช่วย! การมอบหมายงานถาม
เราต้องการให้ทางเลือกแก่ผู้ใช้ของเราในการกรองคำหยาบคายออกไป สมมติว่าเราถือว่าคำว่า แมว สุนัข และลามะ เป็นคำหยาบคาย เขียนโปรแกรมที่อ่านสตริงจากแป้นพิมพ์และทดสอบว่าสตริงนั้นมีคำหยาบคายคำใดคำหนึ่งของเราหรือไม่ ให้โปรแกรมของคุณปฏิเสธเฉพาะบรรทัดที่มีคำหยาบคายเท่านั้น ตัวอย่างเช่น การต่อข้อมูลแบบ Dogmatic เป็นหมวดหมู่ขนาดเล็ก ประโยคนี้ไม่ควรถือเป็นคำหยาบคาย ฉันรู้ว่า ==0 ผิด แต่ฉันอยากให้ไม่มีคำว่าหายนะ ไม่ดูหมิ่น และพิมพ์ประโยคเช่น "พายุเป็นหายนะ" และอย่าดูหมิ่น
import java.util.*;
public class Profanity {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String words;
System.out.println("Enter a sentence");
words = in.nextLine();
words = words.toLowerCase();
if (words.indexOf("cat") !=-1)
{
if(words.length()==3)
System.out.println("cats by itself censored!");
else if(words.indexOf("cat ")==0)
System.out.println("cat is in the beginning censored!");
else if(words.indexOf(" cat ")!=-1)
System.out.println("cat is in the string censored!");
else if(words.indexOf(" cat")==0); //<-- ==0 is def wrong, please help
System.out.println("cat is at the end censored!"); }
else
System.out.println(words);
}}