在这里,我将基本字符串称为String baseString = "nullpointer";如果用户输入了pointernullllpointerinternullpo之类的字符串,则应输出TRUE,否则为FALSE。如何为此编写逻辑?请帮我。

public class literalString {
public static void main(String[] args) {
    String baseString = "nullpointer";
    Scanner sc = new Scanner(System.in);
    String input = sc.next();
    //pointernull, llpointer, internullpo  then return TRUE.
    // pointerllnu, unpointerll  then return FALSE
    //what shoul be the logic here?
 }
}


您的帮助将不胜感激。

最佳答案

首先检查baseString.length==input.length,如果失败则为错误
如果是真的,则使用concatcontains

简单的例子

String baseString = "nullpointer";
    Scanner sc = new Scanner(System.in);
    String input = sc.next();
    if(baseString.length()!=input.length()){
        System.out.println("false");
    }
    else{
        if(baseString.concat(baseString).contains(input)){
            System.out.println("true");
        }
        else{
            System.out.println("false");
        }
    }

10-07 21:07