Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删失条件_Java - Fatal编程技术网

Java 删失条件

Java 删失条件,java,Java,我需要这个程序来打印审查,如果userInput包含单词darn,否则打印userInput,以换行符结尾 我有: import java.util.Scanner; public class CensoredWords { public static void main (String [] args) { String userInput = ""; Scanner scan = new Scanner(System.in); use

我需要这个程序来打印审查,如果userInput包含单词darn,否则打印userInput,以换行符结尾

我有:

import java.util.Scanner;

public class CensoredWords {
    public static void main (String [] args) {
        String userInput = "";
        Scanner scan = new Scanner(System.in);
        userInput = scan.nextLine;

        if(){
            System.out.print("Censored");
        }    
        else{
            System.out.print(userInput);
        }

        return;
    }
}
不确定if的条件是什么,我认为string类中没有contains方法。

尝试以下方法:

if(userInput.contains("darn"))
{
System.out.print("Censored");
}
          if (userInput.indexOf("darn") > 0) {
             System.out.println("Censored");
          }

          else {
             System.out.println(userInput);
是的,没错,String类有一个名为contains的方法,该方法检查子字符串是否是整个字符串的一部分

if(userInput.contains("darn"))
{
System.out.print("Censored");
}
          if (userInput.indexOf("darn") > 0) {
             System.out.println("Censored");
          }

          else {
             System.out.println(userInput);

是的,没错,String类有一个名为contains的方法,它检查子字符串是否是整个字符串的一部分。最好的解决方案是使用带有单词边界的正则表达式

ifmyString.matches.*?\\b警告\\b.*

这会阻止您将SDARN匹配为粗鲁的单词:
最好的解决方案是使用带有单词边界的正则表达式

ifmyString.matches.*?\\b警告\\b.*

这会阻止您将SDARN匹配为粗鲁的单词:
Java字符串类确实有一个方法。它接受CharSequence对象。检查.

Java字符串类是否有方法。它接受CharSequence对象。检查。

另一个初学者方法是使用indexOf函数。试试这个:

          if (userInput.indexOf("darn") > 0) {
             System.out.println("Censored");
          }

          else {
             System.out.println(userInput);

另一个初学者方法是使用indexOf函数。试试这个:

          if (userInput.indexOf("darn") > 0) {
             System.out.println("Censored");
          }

          else {
             System.out.println(userInput);

哦,有一个contains方法,哇,我的书中甚至没有列出它。谢谢。@初学者代码010812如果您觉得答案是正确的,请接受它作为正确答案,否则它应该列在未回答的问题中。您忘了提到它不接受字符串参数。@初学者代码010812-实际上包含不是这里使用的正确方法。如果你的审查词是粗鲁的,那么contains将返回true以表示谨慎。这就是你想要的吗?@max777是的,你是对的,它接受一个CharSequence,但我们可以将一个字符串作为参数传递,它会自动进行转换,因此不需要太多提及它:哦,有一个contains方法,哇,我的书中甚至没有列出它。谢谢。@初学者代码010812如果您觉得答案是正确的,请接受它作为正确答案,否则它应该列在未回答的问题中。您忘了提到它不接受字符串参数。@初学者代码010812-实际上包含不是这里使用的正确方法。如果你的审查词是粗鲁的,那么contains将返回true以表示谨慎。这就是您想要的吗?@max777是的,您是对的,它接受CharSequence,但我们可以将字符串作为参数传递,它会自动进行转换,因此无需太多提及:不要使用string.contains。。它不会寻找单词边界。i、 如果你的琴弦是谨慎的,那么粗鲁、拘谨和谨慎都是匹配的。所以,你的代码会说谨慎是个坏词。不要使用string.contains。。它不会寻找单词边界。i、 如果你的琴弦是谨慎的,那么粗鲁、拘谨和谨慎都是匹配的。所以,你的代码会说谨慎是个坏词。