程序错误 //此程序确定输入字符串是否为回文 导入java.util.*//从java.util类导入所有方法 导入静态java.lang.System.out; 公共级帕林罗马酒店{ 公共静态void main(字符串[]args){ @抑制警告(“资源”) 扫描仪输入=新扫描仪(System.in); 线状苍白色素; println(“输入字符串:”); pallindrome=input.nextLine(); ArrayList pall=新的ArrayList(); buildAL(pall,pallindrome); 显示器(pall); if(isPalendrome(pall)) println(pallindrome+“是一个pallindrome”); 其他的 out.println(pallindrome+“不是pallindrome”); } 静态无效显示(ArrayList arr1){//此方法用于显示数组列表 for(int i=0;i
问题不清楚,但for循环不会覆盖程序错误 //此程序确定输入字符串是否为回文 导入java.util.*//从java.util类导入所有方法 导入静态java.lang.System.out; 公共级帕林罗马酒店{ 公共静态void main(字符串[]args){ @抑制警告(“资源”) 扫描仪输入=新扫描仪(System.in); 线状苍白色素; println(“输入字符串:”); pallindrome=input.nextLine(); ArrayList pall=新的ArrayList(); buildAL(pall,pallindrome); 显示器(pall); if(isPalendrome(pall)) println(pallindrome+“是一个pallindrome”); 其他的 out.println(pallindrome+“不是pallindrome”); } 静态无效显示(ArrayList arr1){//此方法用于显示数组列表 for(int i=0;i,java,Java,问题不清楚,但for循环不会覆盖word中的字母,因为终止条件基于传递给buildAL方法的空列表大小。替换 //This program determines if the input string is a palindrome import java.util.*;//importing all the methods from java.util class import static java.lang.System.out; public class Pallindrome {
word
中的字母,因为终止条件基于传递给buildAL
方法的空列表
大小。替换
//This program determines if the input string is a palindrome
import java.util.*;//importing all the methods from java.util class
import static java.lang.System.out;
public class Pallindrome {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input= new Scanner(System.in);
String pallindrome;
out.println("Enter a string: ");
pallindrome= input.nextLine();
ArrayList<String> pall= new ArrayList<String>();
buildAL(pall, pallindrome);
display(pall);
if(isPalendrome(pall))
out.println(pallindrome + " is a pallindrome");
else
out.println(pallindrome + " is not a pallindrome");
}
static void display(ArrayList<String> arr1){ //this method is for displaying the array list
for(int i=0; i<arr1.size();i++)
out.print(arr1.get(i));
out.println();
}
static void buildAL(ArrayList<String> arr2, String word){ //this is for building the array with the entered word
for(int i=0;i<arr2.size();i++)
arr2.add(word.charAt(i)+ "");
}
static Boolean isPalendrome(ArrayList<String> arr3){ //it will test if the word is pallindrome
ArrayList<String> rarr3= new ArrayList<String>();
rarr3.addAll(arr3);
Collections.reverse(rarr3);
for(int i=0;i<rarr3.size();i++)
if(!(rarr3.get(i).equals(arr3.get(i))))
return false;
return true;
}
}
for(int i=0;i
与
for(int i=0;i
如下
for (int i = 0; i < word.length(); i++) {
您需要告诉我们错误,我们才能告诉您原因,但是您知道
StringBuilder
有一个reverse()
方法,对吗?而且,这个词是回文的。
for (int i = 0; i < word.length(); i++) {
static void buildAL(ArrayList<String> arr2, String word){
for(int i=0;i<arr2.size();i++)
arr2.add(word.charAt(i)+ "");
}
StringBuilder sb = new StringBuilder("your String");
if ("yourString".equals(sb.reverse().toString())) {
//or you can use equalsIgnoreCase also if that fits your requirement
//its a palindrome
} //Otherwise, not.