问题是如果输入123 反过来读是321 123!=321 输出NO 如果输入323 反过来读是323 323=323 输出YES
我编了一个程序,编译通过了但是不出结果大家帮我改一下,谢谢,我是新手!import java.io.*;
public class Q42
{
public static void main(String args[])
{
try
{
BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
int X;
int sum=0;
String s="";
String st="";
int i=0;
System.out.println("X=");
X=Integer.parseInt(keyboard.readLine());
while (X!=0)
{
sum=X%10+sum;
st=Integer.toString(X%10);
X=X/10;
s=s+st;
}
i=Integer.parseInt("s");
if (i==X)
{
System.out.print("X is a palindrome");
}
else
System.out.print("X is Not a palindrome");
}
catch(Exception e){}
}
}
我编了一个程序,编译通过了但是不出结果大家帮我改一下,谢谢,我是新手!import java.io.*;
public class Q42
{
public static void main(String args[])
{
try
{
BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
int X;
int sum=0;
String s="";
String st="";
int i=0;
System.out.println("X=");
X=Integer.parseInt(keyboard.readLine());
while (X!=0)
{
sum=X%10+sum;
st=Integer.toString(X%10);
X=X/10;
s=s+st;
}
i=Integer.parseInt("s");
if (i==X)
{
System.out.print("X is a palindrome");
}
else
System.out.print("X is Not a palindrome");
}
catch(Exception e){}
}
}
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in)); StringBuffer X; System.out.println("X=");
X = new StringBuffer(keyboard.readLine()); String uX = new String(X);
X.reverse();
if (uX.equals(X.toString())) {
System.out.println("X is a palindrome");
} else{
System.out.println("X is Not a palindrome");
}
} catch (Exception e) {
}
i=Integer.parseInt("s");
改成
i=Integer.parseInt(s);
就好了自己在看看吧,还有尽量在catch中加一句
e.printStackTrace();
会给调试还来方便.
{
public static void main(String args[])
{
try {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); int X;
int sum = 0;
String s = "";
String st = "";
int i = 0;
System.out.println("X=");
X = Integer.parseInt(keyboard.readLine());
int x = X; while (X != 0) {
sum = X % 10 + sum;
st = Integer.toString(X % 10);
X = X / 10;
s = s + st;
System.out.println(s);
} i = Integer.parseInt(s);
System.out.println("i = " + i );
System.out.println("X = " + X );
if (i == x) {
System.out.print("X is a palindrome");
}
else {
System.out.print("X is Not a palindrome");
}
}
catch (Exception e) {
e.printStackTrace();
}
}但觉得5楼的做法还是比较好的.
public static void main(String[] args) {
String str = readString();
boolean b = isPalindrome(str);
System.out.printf("%s is %sa palindrome.%n", str, b ? "" : "NOT ");
}
public static String readString() {
Scanner sc = new Scanner(System.in);
System.out.print("Please input a string: ");
String str = sc.next().trim();
sc.close();
return str;
}
public static boolean isPalindrome(String str) {
char[] chars = str.toCharArray();
int maxIndex = str.length() - 1;
for(int i = 0, k = str.length() / 2; i < k; i++) {
if(chars[i] != chars[maxIndex - i]) {
return false;
}
}
return true;
}
}
但是谢谢大家的帮忙,怎么才能写出他那种程序呢?
public static void main(String[] args) {
String str = readString();
boolean b = isPalindrome(str);
// C 风格的格式输出,看其中的%s(表示字符串),
// 后面参考的第一个填充第一个%s,第二个填充第二个%s,
// 第二个是个三目运算符,当b=true时,设为空的,为false时设为NOT
// 这样就可以用于显示了。
System.out.printf("%s is %sa palindrome.%n", str, b ? "" : "NOT ");
}
/**
* 从控制台输入字符串
* @return 输入的字符串
*/
public static String readString() {
// Scanner是JDK1.5中新增加的类,可以很方便地从控制台中读入数据
Scanner sc = new Scanner(System.in);
System.out.print("Please input a string: ");
// next()从控制台中读取数据,并去掉前后的空格
String str = sc.next().trim();
sc.close();
return str;
}
/**
* 判断一个字符串是否是回文<br/>
* 比较方法:<br/>
* 左边第一个字符与右边第一个字符进行比较;<br/>
* 左边第二个字符与右边第二个字符进行比较;<br/>
* ……<br/>
* 直到全部比完,如果都是相等的话,那就是回文的,否则就不是了
* @param str 待判断的字符串
* @return true 是回文的;false 不是回文的
*/
public static boolean isPalindrome(String str) {
// 将字符串罻为字符数组,就变成一个一个的了
char[] chars = str.toCharArray();
// 获得字符串中最大的索引值
int maxIndex = str.length() - 1;
// 由于左边和右边进行比较,只要循环一半就够了
for(int i = 0, k = str.length() / 2; i < k; i++) {
if(chars[i] != chars[maxIndex - i]) {
// 当左右字符只要有一个不同时,那肯定就不是回文的了,直接返回false
return false;
}
}
// 全部完成后,说明都是相同的,那就是回文,返回true了
return true;
}
}我再回答一下,你在9楼的疑问。要尽量将一个方法的功能写得简单些,
这要这个方法才有可能被重用,尽量能抽取出今后可能公用的部分。
像上面的那个程序,就可以进行拆分成:从控制台中读入字符串和判断
是否是回文的,这样拆开来的好处就是:万一哪一天不采用控制台读了,
而采用从文件中读,这样你的程序都进行改动,而且采用模块化的话,
只要再增加一个从文件中读取字符串的方法就可以了,别人想从哪读入
字符串就调用合适的方法就可以了,不知道这样说是否有点明白了?