编写一个需要查看字符串并判断两个字母是否相邻的方法

我正在为学校做一个实验室,这些是说明:“编写一个程序来查看一个字符串是否包含另一个特定字母旁边的特定字母。”


这是示例数据:


要输入的示例数据:


chicken a b

frog f g

chicken c k

apluscompsci a s

apluscompsci a p

apluscompsci s c

apluscompsci c p

这是示例输出:


false

false

true

false

true

true

false

到目前为止,这是我的代码:


public class AB

{

    public static boolean check( String s, String a, String b)

    {

        int i;

        if(s.indexOf(a) == -1 || s.indexOf(b) == -1)

        {

            return false;

        }

        if(s.indexOf(a) != -1 || s.indexOf(b) != -1)

        {

            for(i = 0; i < s.length() - 1; i++)

            {

                if(s.substring(s.indexOf(a) - 1,s.indexOf(a)).equals(b) ||  s.substring(s.indexOf(a) + 1,s.indexOf(a)).equals(b) || s.substring(s.lastIndexOf(a) - 1,s.indexOf(a)).equals(b) || s.substring(s.lastIndexOf(a) + 1,s.indexOf(a)).equals(b));

                {

                    return true;

                }

            }  

        }

        return false;

    }

}

这是我的跑步者的样子:


public class AplusRunner

{

    public static void main( String args[] )

    {

        System.out.println( AB.check( "chicken", "a", "b" ) );

        //add more cases

        System.out.println( AB.check( "frog", "f", "g" ) );

        System.out.println( AB.check( "chicken", "c", "k" ) );

        System.out.println( AB.check( "apluscompsci", "a", "s" ) );

        System.out.println( AB.check( "apluscompsci", "a", "p" ) );

        System.out.println( AB.check( "apluscompsci", "s", "c" ) );

        System.out.println( AB.check( "apluscompsci", "c", "p" ) );

    }

}

出于某种原因,它一直显示第一个字符串样本为假(这应该发生),但对于第二个字符串样本,字符串索引超出 -1 的范围,我不知道如何解决这个问题。感谢您的任何帮助!


函数式编程
浏览 298回答 3
3回答

SMILET

我觉得你做了一个不必要的验证检查,你可以简化如下。抱歉没有为参数使用有意义的名称。尝试从您的角度重新实现这一点。public class AB {&nbsp; public static boolean isConsecutive(String s, String a, String b) {&nbsp; &nbsp; return s.indexOf(a + b) != -1;&nbsp;}&nbsp;public static void main(String args[]) {&nbsp; &nbsp; System.out.println(AB.isConsecutive("chicken", "a", "b"));&nbsp; &nbsp; System.out.println(AB.isConsecutive("frog", "f", "g"));&nbsp; &nbsp; System.out.println(AB.isConsecutive("chicken", "c", "k"));&nbsp; &nbsp; System.out.println(AB.isConsecutive("apluscompsci", "a", "s"));&nbsp; &nbsp; System.out.println(AB.isConsecutive("apluscompsci", "a", "p"));&nbsp; &nbsp; System.out.println(AB.isConsecutive("apluscompsci", "s", "c"));&nbsp; &nbsp; System.out.println(AB.isConsecutive("apluscompsci", "c", "p"));&nbsp;}}输出 :falsefalsetruefalsetruetruefalse

智慧大石

String 类已经提供了许多实用方法来执行多个操作,为什么不使用它们而不是重新发明轮子呢?只需将您的方法更改为public static boolean check (String s, String a, String b) {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// check if combination of both strings (a+b) are present in 's'&nbsp;&nbsp; &nbsp;return (s.contains(a+b)) ? true : false;}

慕容森

您可以通过组合各个字母并检查它们的索引来简单地创建一个临时字符串。这段代码应该适合你。&nbsp; &nbsp; import java.util.*;public class Letters{&nbsp; &nbsp; private static boolean CheckConsecutiveLetters(String str, String l1, String l2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int index_l1 = str.indexOf(l1);&nbsp; &nbsp; &nbsp; &nbsp; String temp = l1 + l2;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(temp);&nbsp; &nbsp; &nbsp; &nbsp; if(str.indexOf(temp) >= 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; boolean output = CheckConsecutiveLetters("chicken", "c", "k" );&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(output);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java