用JAVA编写程序检查是否为回文单词

来源:百度知道 编辑:UC知道 时间:2024/07/05 22:01:15
下面是我编写的程序,有些问题请指教一下。运行后会出现这样的话:需要“class”或“interface”
public static void main (String args[])一个错误
这里是我编写的程序:
public class d
{
public static String reverse(String s)
{
int charNumbers=s.lenght();
char[] chars=new char[charNumbers];
s.getchar(0,charNumbers,chars,0);
String result=" " ;
for(int i=charNumbers-1;i>=0;i--)
{
result=result+chars[i];}
return result;
}
}

public static void main(String args[])
{
String word ="noon";
if(word.equals(reverse(word)))
{System.out.println(word+"是一个回文单词!");}
else
{System.out.println(word+"不是一个回文单词!");}
}

老大,
首先,这一行
int charNumbers=s.lenght();

s.length(),不是lenght();
s.getChars(),不是s.getchar

最重要的一点,你把main方法写在类定义的外面……

public class d
{
public static String reverse(String s)
{
int charNumbers=s.length();
char[] chars=new char[charNumbers];
s.getChars(0,charNumbers,chars,0);
String result=" " ;
for(int i=charNumbers-1;i>=0;i--)
{
result=result+chars[i];}
return result;
}

public static void main(String args[])
{
String word ="noon";
if(word.equals(reverse(word)))
{System.out.println(word+"是一个回文单词!");}
else
{System.out.println(word+"不是一个回文单词!");}
}
}