【发布时间】:2014-07-20 14:50:56
【问题描述】:
我正在尝试编写一个将 long 转换为字符串并检查它是否是回文的程序,到目前为止我已经编写了以下内容,但是它给了我一个不兼容的类型错误,我似乎无法找出导致它的原因。 :S 任何帮助将不胜感激:)
错误发生在第 24 行,它说类型不兼容 - found void but expected java.lang.String
public class programPalindrome
{
private String go()
{
Input in = new Input ();
System.out.print("Enter Number: ");
return in.nextLine();
long number = in.nextLong();
String Palindrome = Long.toString(number); // converts the long into a string
String newAnswer = reverse(Palindrome);
String anotherAnswer = reverseCheck(Palindrome,newAnswer);
System.out.println("This is a Palindrome" + Palindrome);
}
// Check to see if the two argument Strings are the reverse of each
// other.
private String reverseCheck(String Palindrome, String newAnswer)
{
if (Palindrome.compareTo(newAnswer) == 0) {
return System.out.println("It is a palindrome");
}
else
{
return System.out.println("It is not a palindrome");
}
}
// Return a String which is the reverse of the argument String
private String reverse(final String Palindrome)
{
String result = new String();
int position = 0;
while (position < Palindrome.length())
{
result = new Character(newAnswer.charAt(position)).toString() + result;
position = position + 1;
}
return result;
}
public static void main(String[] args)
{
new programPalindrome().go();
}
}
【问题讨论】:
-
你能发布堆栈跟踪吗?
-
请添加错误信息和发生的行。
-
编译失败在哪里?请问可以添加stackTrace吗?
-
你从函数的中间返回——
go()中的第三行 -
为什么人们要编写代码来做到这一点?那里有一些该死的东西需要测试吗?
标签: java