public static void main(String[] args) {
String input = new String(JOptionPane.showInputDialog("Enter a string."));
String inputLow = input.toLowerCase();
String output = ""; //Blank string
for(int i = 0; i < inputLow.length(); i++) //For loop to continue through the entered string
{
if(inputLow.charAt(i) >= 'a' && inputLow.charAt(i) <= 'z') //If statement to check for appropriate characters only
{
output += inputLow.charAt(i); //Add only the appropriate characters to String output ('a' - 'z')
}
}
System.out.println(output);
//GETTING REVERSE STRING
int n = output.length() - 1;
String last = "";
for(int k = n; k >= 0; k--)
{
System.out.print(output.charAt(k));
//last = String.valueOf(output.charAt(k));
}
//System.out.println("");
//System.out.println(last);
}因此,我试图最后打印字符串,但当我不注释该代码时,它会输出以下内容:
heyman
namyeh
h但我想把“海曼”印在第三行。(我只是做print语句来测试它是否正确,我的目标是比较最后一个字符串输出和字符串输出,如果它们是相同的,那么它就是一个回文,否则就不是。)
我如何使用这个方法(或者类似的方法)来完成这个任务?
发布于 2014-04-14 15:51:03
你在一次设置最后一个字符的值。基本上每次都要重置它,这就是为什么它以反向字符串的最后一个字符结束(这是第一个字符)。
将其更改为last = String.valueOf(output.charAt(k)) + last;
https://stackoverflow.com/questions/23064481
复制相似问题