Topic: 找不到原因(回文数 如输入1234 则输出4321)

  Print this page

1.找不到原因(回文数 如输入1234 则输出4321) Copy to clipboard
Posted by: zhuzi0475
Posted on: 2005-10-09 10:01

请尽量用准确的文字描述作为标题


程序如下:没有错误但不能实现我的目的(回文数 如输入 1234 则输出4321 )不要求格式只求能出结果,请高手指点。

import java.applet.Applet;
import java.awt.*;

public class div extends Applet{
  Label prompt1;
  Label prompt2;
  TextField tf1;
  
  
  public void init()
  {
    prompt1=new Label("sr");
    prompt2=new Label("jgs:");
    tf1=new TextField(10);
    add(prompt1);
    add(prompt2);
    add(tf1);
  }
  public boolean action (Event e,Object o)
  {
    char s[]=new char[10];
    int t[]=new int[10];
    if(e.target==tf1)
    {
     for(int i=0;i<tf1.getText().length();i++)
     {
     s[i]=(char)(tf1.getText().charAt(i));
     t[i]=(int)s[s.length-i-1];
     }
    }
    for(int j=0;j<t.length;j++)
    {
      prompt2.setText("jgs"+t[j]);  
    }
    
  
    repaint();
    return true;
  }
  
}

2.Re:找不到原因(回文数 如输入1234 则输出4321) [Re: zhuzi0475] Copy to clipboard
Posted by: why
Posted on: 2005-10-09 22:14

I'm not interested in correcting your code but I would suggest using a method to do the 回文 operation, which could be as simple as
String reverse(String string) {
StringBuffer sb = new StringBuffer(string);
return sb.reverse().toString();
}

you may check the implmentation of StringBuffer's reverse() for details


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923