作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
package palindrome;
import java.util.Scanner;
public class Palindrome
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the word = > ");
String word = input.nextLine();
int flag = 0;
int x = word.length();
int i = 0;
int j = x;
for(; i<=x/2 && j>=x/2; i++,j--)
{
if(word.charAt(i)!=word.charAt(j))
{
flag = 1;
break;
}
}
if(flag==0)
{
System.out.printf("The word '%s' is a palindrome", word);
}
else
System.out.printf("The word '%s' is not a palindrome", word);
}
}
输入单词=>女士
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:658)
at palindrome.Palindrome.main(Palindrome.java:16)
最佳答案
int j = x;
应该是:
int j = x-1;
关于java - 回文代码(错误如下),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703531/
我想开发一个 Skype 机器人,它将用户名作为输入,并根据用户输入以相反的字符大小写表示hello username。简而言之,如果用户输入他的名字 james,我的机器人会回复他为 Hello J
我是一名优秀的程序员,十分优秀!