我想编写一个判断是否回文的程式(回文是从前向后和从后向前读都相同的文字,比如12321,45554,11611等).现在编写程式,只是利用while,if/else,if这些结构,读入一个五位数不位数并判断是否回文,如果不是,向用户显示一个错误信息对话框,当用户关闭对话框后,允许用户输入新的值.我现在还未想到算法,请各位给些意见.

解决方案 »

  1.   

    以下是我编写的程式,可是我自己也觉得不对.请大家指导:
    //fig.3-2: Test21.java
    //import javax.swing.JOptionPane;public class Test21 {
       public static void main (String args [])   {
        String A,B,C,D,E;       int a=0,b=0,c=0,d=0,e=0,total=0,gc=0;
        
        while(gc!=-1){
        A= JOptionPane.showInputDialog("A");
        B= JOptionPane.showInputDialog("B");
        C= JOptionPane.showInputDialog("C");
        D= JOptionPane.showInputDialog("D");
        E= JOptionPane.showInputDialog("E");    a= Integer.parseInt(A);
        b= Integer.parseInt(B);
        c= Integer.parseInt(C);
        d= Integer.parseInt(D);
        e= Integer.parseInt(E);   
          if(a=e){
             if(b=d)
             total= a*10000+b*1000+c*100+d*10+e*1;
             System.out.println(total);
          }
          else
          System.out.println("Wrong answer!"); 
          gc+=1
          }
       }
    }
      

  2.   

    //fig.3-2: Test21.java
    //import javax.swing.JOptionPane;public class Test21 {
       public static void main (String args [])   {
        String A,B,C,D,E;       int a=0,b=0,c=0,d=0,e=0,total=0,gc=0;
        
        while(gc!=-1){
        A= JOptionPane.showInputDialog("A");
        B= JOptionPane.showInputDialog("B");
        C= JOptionPane.showInputDialog("C");
        D= JOptionPane.showInputDialog("D");
        E= JOptionPane.showInputDialog("E");    a= Integer.parseInt(A);
        b= Integer.parseInt(B);
        c= Integer.parseInt(C);
        d= Integer.parseInt(D);
        e= Integer.parseInt(E);   
          if(a=e){
             if(b=d)
             total= a*10000+b*1000+c*100+d*10+e*1;
             System.out.println(total);
             gc=-1;
          }
          else
          System.out.println("Wrong answer!"); 
          }
       }
    }
      

  3.   

    在编译的时候出现了错误:
    Test21.java:34: incompatible types
    found   : int
    required: boolean
          if(a=e){
             ^
    Test21.java:34: incompatible types
    found   : int
    required: boolean
          if(b=d)
             ^
    2 errors请问是什么原因?请指导.
      

  4.   

    看看:
    http://community.csdn.net/Expert/TopicView.asp?id=3957171
      

  5.   


    String str = "abcba";
    StringBuffer s = new StringBuffer();
    s.append(str); System.out.println(str.equals(s.reverse().toString())?"yes":"no");
      

  6.   

    以下为正确的答案:
    import javax.swing.JOptionPane;public class Test21 {
       public static void main (String args [])   {
        String A,B,C,D,E;       int a=0,b=0,c=0,d=0,e=0,total=0,gc=0;
        
        while(gc!=-1){
        A= JOptionPane.showInputDialog("A");
        B= JOptionPane.showInputDialog("B");
        C= JOptionPane.showInputDialog("C");
        D= JOptionPane.showInputDialog("D");
        E= JOptionPane.showInputDialog("E");    a= Integer.parseInt(A);
        b= Integer.parseInt(B);
        c= Integer.parseInt(C);
        d= Integer.parseInt(D);
        e= Integer.parseInt(E);   
          if(a==e){
             if(b==d)
             total= a*10000+b*1000+c*100+d*10+e*1;
             System.out.println(total);
             gc=-1;
          }
          else
          System.out.println("Wrong answer!"); 
          }
       System.exit(0);
       }}
      

  7.   

    这是我以前的作业题,输入一个小于80个字的字符串,判断是否回文
    import java.io.*;
    public class TestPalindrome
    {
    public static boolean palindrome(char[] a,int used)
    {
    int i,k;
    k=(int)(used+1)/2;
    for(i=0;i<k;i++)
    {
    if(a[i]!=a[used-1-i])return false;
    }
    return true;
           
    }
        public static void compare()
        {
         String s=new String();
    char[] c=new char[80];
    int i,length,l,n=0;
    boolean b;
    BufferedReader input=new BufferedReader(new InputStreamReader   
                                                               (System.in));
    try
    {
    System.out.println("Input a string(less than 80 
                                                   characters):");
    s=input.readLine();
    if(s.length()>80)
    {
    System.out.println("error:the number is out of  
                                                            bound!");
    System.exit(0);
    }
    }
    catch(IOException e)
    {
    System.out.println(e);
    }
    //change upperletter to lowerletter
    //s=s.toLowerCase();

    length=s.length();
    l=0;
    for(i=0;i<length;i++)
    {
                if(s.charAt(i)==' ')
                   continue;
    c[l]=s.charAt(i);
    l+=1;
    }

    b=palindrome(c,l);

    if(b)
       System.out.println("The string is palindrome.");
    else 
       System.out.println("The string is not palindrome.");
        }
        
    public static void main(String[] args)
    {

     compare();
     char p='n';
     BufferedReader input=new BufferedReader(new InputStreamReader
                                                                 (System.in));   
     for(;;)
     {
      System.out.print("do you want to try again? Y/N  ");
        try
          {
            p=input.readLine().charAt(0);
        }
        catch(IOException e)
        {
          }
        if((p=='y')||(p=='Y'))
            compare();
        else break; 
     }   
    }  
    }