Java汉诺塔问题

来源:百度知道 编辑:UC知道 时间:2024/09/25 12:09:36
package haha;

import java.applet.*;
import java.awt.*;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ji extends Applet implements ActionListener

{
Button aa=new Button("确定");
TextField cc=new TextField(10);
TextArea bb=new TextArea();

public void init()
{
setBackground(Color.orange);
add(aa);add(cc);add(bb);
}
public void move(char x,char y)
{
bb.setText(x+"-->"+y+"\n");
}

public void hanio(int v,char a,char b,char c)
{
hanio(v-1,a,c,b);
move(a,c);
hanio(v-1,b,a,c);
}

public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub

if(e.getSource()==aa)
{
int n;
n=Integer.parseInt(cc.getText());//转换整形
hanio(n,'A',

第一: 你没设置Button aa的ActionListener, 在init里加上 aa.addActionListener(this);

第二:你的递归函数没有终止条件,会是个死循环,比如可以加上if(v>0) hanio(....)

第三:bb TextArea 保留不了历史数据,应该每次先取出旧的再加上新的:
bb.setText(bb.getText() + "......");