doubleX(String someString)方法应根据输入字符串是否包含连续的'x'字符返回布尔值 [英] doubleX(String someString) method should return Boolean based on whether input string contains consecutive 'x' characters

查看:36
本文介绍了doubleX(String someString)方法应根据输入字符串是否包含连续的'x'字符返回布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我有大学的这项工作.我们必须:

I'm very new to Java and I have this assignment for college. We have to:

使用名为doubleX的静态方法创建一个名为DoubleX的类,该方法将String作为参数并返回一个布尔值,如果字符串中的第一个"x"紧随其后是另一个"x",则返回true;否则返回false

Create a class called DoubleX with a static method called doubleX which takes a String as a parameter and returns a boolean value which will be true if the first 'x' in the string is immediately followed by another 'x' and false otherwise.

例如,在字符串"abcdxex"中,第一个"x"后紧跟一个"e",而不是"x",因此该方法将返回false.另一方面,字符串"aaaxxaa"将导致该方法返回true.

For example, in the string, "abcdxex", the first 'x' is immediately followed by an 'e', not an 'x' and therefore the method will return false. On the other hand, the string, "aaaxxaa" will cause the method to return true.

您的方法将由另一个Java类调用,如下所示:(这是由我的讲师提供的)

Your method will be called by another Java class as follows: (this is provided by my lecturer)

public class main
{
   public static void main(String [] args)
   {
      boolean result = DoubleX.doublex("aaaxxaa");
      System.out.println(result);
   }
}

到目前为止,我的代码如下:

So far my code looks like:

import java.util.Scanner;

public class DoubleX
{
     public static void main (String [] args);

     Scanner in = new Scanner(System.in);

     System.out.print("Enter a phrase: ");

     String phrase = in.nextString();  

     }
}

我一直在尝试许多选择,但是没有用.

I've been trying many options but nothing works.

请帮助!

我一直在努力.谁能说我走的路是否正确?它似乎正在工作,但它不断告诉我最后一行 System.out.println(result);

I've been working at it. Can anyone say if I'm on the right track? It seems to be working but it keeps telling me that there is an identifier problem with the last line System.out.println(result);

import java.util.Scanner;

public class DoubleX
{
     public static boolean doubleX (String [] args);
         Scanner in = new Scanner(System.in);
         System.out.println("Please enter a word: ");
         String name = in.next();
         result = DoubleX.doubleX(name);

     {
         if ("xx")
         System.out.println("True");
         else
         System.out.println("False"); 
     }

     System.out.println(result);
}

推荐答案

一旦您从用户那里得到了输入(您做得很好-实际上,这不是分配的一部分),则检查字符串是否在彼此后面紧跟两个X应当很简单(即"xx" ).至少有两种方法可以做到这一点:

Once you've got the input from the user (you did that quite nicely - and actually, that's not part of the assignment), it should be fairly simple to check if the string contains two Xes right behind each others (namely "xx"). There's at least two ways you can do this:

  • 简单:通过使用方法
  • Simple: By using the method String.contains(CharSequence), which checks if a String contains another String
  • Overkill: By using regular expressions to check if your String matches a pattern like <any amount of arbitrary characters>xx<any amount of arbitrary characters>

剩下要做的就是在 DoubleX 中定义一个 public static 方法,其名称为 doublex ,一个 String 参数,返回类型为 boolean (另请参见: @JamesWierzba的评论).

Only thing that's left to do is to define a public static method inside DoubleX with the name doublex, a String argument and a return type of boolean (see also: @JamesWierzba's comment).

这篇关于doubleX(String someString)方法应根据输入字符串是否包含连续的'x'字符返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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