这里我基于名为String baseString = "nullpointer"
的字符串;如果用户输入了一些字符串,例如pointernull
,llpointer
,internullpo
,那么它应该打印为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?
}
}
我们将不胜感激。
答案 0 :(得分:3)
首先检查baseString.length==input.length
是否失败然后是错误的
如果确实如此,请使用concat
和contains
简单示例
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");
}
}
答案 1 :(得分:0)
尝试这样的事情:
String[] valueList = {"pointernull", "llpointer", "internullpo"};
if(Arrays.asList(valueList).indexOf(input) != -1) {
// User has typed "pointernull", "llpointer" or "internullpo"
}else{
// User has typed something else
}
答案 2 :(得分:0)
简单地:
(base.concat(base)).contains(input);
因此,如果您的基数为nullpointer
,则base.concat(base)
将产生nullpointernullpointer
,而contains
将在true
上返回nullp
,{{ 1}},pointernull
等