Java 检查字符串是否为回文时出现问题
我是一个学习Java的初学者,被要求检查给定的字符串是否是回文 以下是我到目前为止的情况:Java 检查字符串是否为回文时出现问题,java,Java,我是一个学习Java的初学者,被要求检查给定的字符串是否是回文 以下是我到目前为止的情况: int namel = name.length(); for (int i =0; i<=namel; i++) { char letter = name.charAt(i); char namerev = name.charAt(namel-i); String letterS =txtNamePali.getText(); i
int namel = name.length();
for (int i =0; i<=namel; i++)
{
char letter = name.charAt(i);
char namerev = name.charAt(namel-i);
String letterS =txtNamePali.getText();
if(letter==namerev)
{
txtNamePali.setText("Palindrone");
}
else
{
txtNamePali.setText( "Not a Palindrone");
}
}
int-namel=name.length();
对于(int i=0;i您可以使用
使用其中的函数。
例如:
输出:
Not Palindrome
Palindrome
简而言之,你可以这样做
new StringBuilder().append(yourString).reverse().toString().equals(yourString)
如果字符串为回文,则返回布尔值true,否则返回false。您可以使用
使用其中的函数。
例如:
输出:
Not Palindrome
Palindrome
简而言之,你可以这样做
new StringBuilder().append(yourString).reverse().toString().equals(yourString)
如果字符串为回文,则返回布尔值true,否则返回false。您的代码运行正常,但正如一些人所说,您需要解释一些错误,这些错误应显示为编译器问题
int namel = name.length();
boolean isPalindrome = true;
//add a tracking value, it's a palindrome unless we prove it otherwise
for (int i =0; i< namel/2; i++)
//change from <= to < because arrays are 0-index, we also only have to check halfway so we can use namel/2
{
char letter = name.charAt(i);
char namerev = name.charAt(namel-i);
//String letterS =txtNamePali.getText(); <-- not sure what this was for, possibly a debug statement
if(letter!=namerev)
{
isPalindrome = false; //we have found a non-matching value, it'll stay false, and we'll output correctly
}
}
//then we set the text once. Keeping the text inside would have returned an erroneous "abbc" is a palindrome.
if(isPalindrome) {
txtNamePali.setText("Palindrone");
}
else {
txtNamePali.setText( "Not a Palindrone");
}
int-namel=name.length();
布尔值isAlindrome=true;
//添加一个跟踪值,它是一个回文,除非我们证明它不是
对于(int i=0;i //从更改您的代码在正确的轨道上,但是正如一些人所说,您需要解释一些错误,这些错误应该作为编译器问题出现
int namel = name.length();
boolean isPalindrome = true;
//add a tracking value, it's a palindrome unless we prove it otherwise
for (int i =0; i< namel/2; i++)
//change from <= to < because arrays are 0-index, we also only have to check halfway so we can use namel/2
{
char letter = name.charAt(i);
char namerev = name.charAt(namel-i);
//String letterS =txtNamePali.getText(); <-- not sure what this was for, possibly a debug statement
if(letter!=namerev)
{
isPalindrome = false; //we have found a non-matching value, it'll stay false, and we'll output correctly
}
}
//then we set the text once. Keeping the text inside would have returned an erroneous "abbc" is a palindrome.
if(isPalindrome) {
txtNamePali.setText("Palindrone");
}
else {
txtNamePali.setText( "Not a Palindrone");
}
int-namel=name.length();
布尔值isAlindrome=true;
//添加一个跟踪值,它是一个回文,除非我们证明它不是
对于(int i=0;i //我认为最简单的测试方法是用这个词来构造输入的倒序。另外,这个词通常拼写为回文
我认为最简单的测试是用这个词来构造输入的倒序。另外,这个词通常拼写为回文
1.该方法的结果应在检查整个字符串时给出。因此,首先将
if(letter==namerev)
{
txtNamePali.setText("Palindrone");
}
else
{
txtNamePali.setText( "Not a Palindrone");
}
在循环之外(并更改条件,如下面我的建议中所述)。
当第一次出现两个字符不匹配时,可以中断循环
2.您不再使用char namerev=name.charAt(namel-i);
而是将位置再减少一次。
所以使用:charnamerev=name.charAt(namel-1-i);
试着这样做:
String s = "stringtotest";
boolean result = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(s.length()-1-i)) {
result = false;
break;
}
}
if (result)
System.out.println("Palindrom");
else
System.out.println("Not palindrom");
String s=“stringtotest”;
布尔结果=真;
对于(int i=0;i
1.该方法的结果应在检查整个字符串时给出。因此,首先
if(letter==namerev)
{
txtNamePali.setText("Palindrone");
}
else
{
txtNamePali.setText( "Not a Palindrone");
}
在循环之外(并更改条件,如下面我的建议中所述)。
当第一次出现两个字符不匹配时,可以中断循环
2.您不再使用char namerev=name.charAt(namel-i);
而是将位置再减少一次。
所以使用:charnamerev=name.charAt(namel-1-i);
试着这样做:
String s = "stringtotest";
boolean result = true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(s.length()-1-i)) {
result = false;
break;
}
}
if (result)
System.out.println("Palindrom");
else
System.out.println("Not palindrom");
String s=“stringtotest”;
布尔结果=真;
对于(int i=0;i
String name=“pop”//String检查是否回文
字符串revName=“”;
int namel=name.length();
for(int i=1;iString name=“pop”//String检查它是否回文
字符串revName=“”;
int namel=name.length();
对于(int i=1;i来说,与其他方法的总体思路相同,但我认为这更清晰、更易于阅读。只是一个想法——每个人都有自己的风格。:)如果你是初学者,我强烈建议养成习惯,将冗余逻辑移到自己的方法中。它更清晰,以后可能会更有用
public class Main {
public static void main( String args[] ) {
for ( String string : args ) {
if ( isPalendrome( string ) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
}
}
private static boolean isPalindrome( String string ) {
return string.equals( reverse( string ) );
}
private static String reverse( String original ) {
return new StringBuilder(original).reverse().toString();
}
}
与其他人的总体思路相同,但我认为这更清晰、更容易阅读。只是一个想法——每个人都有自己的风格。:)如果你是初学者,我强烈建议养成习惯,将冗余逻辑转移到自己的方法中。它更清晰,以后可能会更有用
public class Main {
public static void main( String args[] ) {
for ( String string : args ) {
if ( isPalendrome( string ) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
}
}
private static boolean isPalindrome( String string ) {
return string.equals( reverse( string ) );
}
private static String reverse( String original ) {
return new StringBuilder(original).reverse().toString();
}
}
可能需要从查看索引开始。假设您有一个5个字母长的单词,因此namel==5,您的代码正在尝试这样做:name.charAt(0)和name.charAt(5)。我想这会导致您的代码出错……您需要将循环更改为I0
开始,以length()结束-1
。对于初学者来说,这是一个相当大的陷阱;我不确定做这个练习的人是否意识到同一个Unicode代码点可能由多个char
…可能希望从查看索引开始。假设你有一个5个字母长的单词,所以namel==5,你的代码正在尝试这样做:name.charAt(0)和name.charAt(5)。我想这会导致您的代码出错……您需要将循环更改为I0开始,以length()结束-1
。对于初学者来说,这是一个相当大的陷阱;我不确定做练习的人是否意识到同一个Unicode代码点可能由多个char
表示。。。