亵渎过滤错误 [英] Profanity filter error

查看:138
本文介绍了亵渎过滤错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望为用户提供过滤亵渎性语言的选项。假设我们认为猫,狗和美洲驼这两个词是亵渎的。编写一个从键盘读取字符串的程序,并测试该字符串是否包含我们的一个亵渎性词语。你的程序应该找到像cAt这样的单词,区别仅在于大小写。

We would like to give our users the option of filtering out profanity . Suppose that we consider the words cat, dog, and llama to be profane. Write a program that reads a string from the keyboard and tests wether the string contains one of our profane words. Your program should find words like cAt that differ only in case.

尝试:

import java.util.Scanner;

public class Assign5 {
    public static void main(String[] args) {
        String cat,dog,llama,x;
        System.out.println("Enter a word");
        Scanner keyboard = new Scanner (System.in);

        x = keyboard.next();
        x.equalsIgnoreCase(x);

        if(x.indexOf("cat")!=-1&&x.indexOf("dog")!=-1&&x.indexOf("llama")!=-1);
        {
            System.out.println("Profanity Detected");
        }
        else
        {
            System.out.println("No Profanity Detected");     
        } 
    }

它强调 else 由于某种原因所以我无法运行它。我做错了什么?

It's underlining else for some reason so I can't run it. What am I doing wrong?

我不允许使用:for,while和array。我们尚未达到这些目标。我只允许使用:boolean,switch和if-else。另外,我使用 x.equalsIgnoreCase(x)来丢弃案例识别。它会起作用吗?

I am not allowed to use: for, while, and arrays. We haven't gotten to those yet. I am only allowed to use: boolean, switch, and if-else. Also, I am using x.equalsIgnoreCase(x) to discard case identification. Will it work?

推荐答案

问题在于:

if (x.indexOf("cat") != -1 
    && x.indexOf("dog") != -1 
    && x.indexOf("llama") != -1); // <-- look there

{
    System.out.println("Profanity Detected");
}

您是否注意到if结尾处有什么内容?

Do you notice something at the end of the "if"?

提示{block}不属于if!这就是导致其他强调的原因。
也许还有其他错误,但这是一个语法错误。

Hint the "{block}" isn't part of the if! This is what is causing the "else" to underline. Perhaps there are additional errors as well, but that's a syntax error flat-out.

我这样格式化我的代码所以我可以轻松地发现这些错误。采用良好的代码格式是一项非常高效的任务:)

I format my code like this so I can spot these errors easily. Adopting good code formatting is a very productive task :)

if (expr) {
   ...
} else {
   ...
}

这篇关于亵渎过滤错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆