import java.lang.String;
public class Word
{
/**
* constructs a Word with String value s
* @param s is string value of Word
*/
public Word(String s)
{
original = s;
}
/**
* reverses letters in original string
* @return a string that is a reverse of original
*/
public String reverse()
{
String temp = original;
String areverse = "";
int x;
for (x = temp.length() ; x>0 || x==0 ; x --)
{
areverse = temp.substring(x);
}
return areverse;
}
/**
* determines is word is a palindrome
* @return true if word is a palindrome, false otherwise
*/
public boolean isPalindrome()
{
boolean flag = false;
String temp = original;
if (temp.equals(temp.reverse()))
flag = true;
return flag;
}
/**
* Alternate method to determine if word is a palindrome
* @return true if word is a palindrome, false otherwise
*/
public boolean isPalindrome2()
{
String temp = original;
int x = temp.length();
boolean flag = false;
int y = 0;
while (temp.subtring(y).equals(temp.substring(x)) && (x>0 || x==0))
{
x--;
y++;
}
if (x==0)
flag=true;
return flag;
}
private String original;
}
我必須編寫這個程序來查找單詞的反轉,並以兩種不同的方式確定單詞是否是迴文。我只給了方法名稱和方法的評論,但方法中的所有代碼都是我的。當我在第一個迴文方法中使用reverse()方法時,bluej告訴我它找不到變量或方法'reverse',儘管我在代碼中早先定義了它。我的問題是什麼?謝謝爲什麼不識別我的方法?
這不包括,但是這是在頂部導入java.lang.String; public class Word { public word(String s) { original = s; } – rxTT
[橡皮鴨調試](http://www.rubberduckdebugging.com/)會在這裏很長的路要走。 – rmlan
我想我們需要查看實際報告錯誤的位置(您在調用'reverse()'方法的位置)。 – markspace