This is my function
public static String isPalindrome(String str) {
String test = "";
String anotherStirng="";
for (int i = str.length()-1; i >= 0; i--) {
test = test + str.charAt(i);
}
return anotherStirng = (str.equals(test) ? "yes" : "no");
}
main is here ...
Scanner scanner = new Scanner(System.in);
int numberOfInput = scanner.nextInt();
String str ="";
String result="";
for (int i=0;i<numberOfInput;i++){
str = scanner.nextLine();
result = isPalindrome(str);
System.out.println(result);
当我在控制台中输入输入数量(例如 3 或 4)时,它会自动说"is",然后工作正常
最佳答案
你忘记了“scanner.nextLine();”在读取 int 值后的代码中。 解释为什么你需要这条线是 Here
Scanner scanner = new Scanner(System.in);
int numberOfInput = scanner.nextInt();
scanner.nextLine(); // ADDED LINE
String str ="";
String result="";
for (int i=0;i<numberOfInput;i++){
str = scanner.nextLine();
result = isPalindrome(str);
System.out.println(result);
}
关于java - 我发现我的输出有一个小问题!谁能说出我的代码发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53746104/