美文网首页
聪聪工作室---JAVA入门小程序---难题小程序

聪聪工作室---JAVA入门小程序---难题小程序

作者: 繁花流水congcong | 来源:发表于2016-07-14 16:52 被阅读25次

Given a string, return true if the first instance of "x" in the string is immediately followed by another "x".

doubleX("axxbb") → true

doubleX("axaxax") → false

doubleX("xxxxx") → true

public class doubleX {

public static void main(String[] args) {

boolean str=doubleX("axxaxaxaxaxa");

System.out.println(str);

}

////////////////////此题很难理解,大难点//////////////////////////////////////////////////////////

private static boolean doubleX(String str) {

int i=str.indexOf("x");//确定x所在的位置,并把x所在的位置定义为i

if(i==-1)return false;//x==-1代表x不存在

String x=str.substring(i);//定义x=x所在的位置  截取字符串i,i代表x所在的位置,相当于一个坐标,str.substring(i)就是把x截取出来=String x

return x.startsWith("xx");//返回 x所在的位置是有xx两个x为开始的为true

}

}

相关文章

网友评论

      本文标题:聪聪工作室---JAVA入门小程序---难题小程序

      本文链接:https://www.haomeiwen.com/subject/rvgejttx.html