Topic: 大侠帮我看看有错没?(汉诺塔 Applet)

  Print this page

1.大侠帮我看看有错没?(汉诺塔 Applet) Copy to clipboard
Posted by: playulin
Posted on: 2006-03-31 20:44

新手上路,整了个汉诺塔的小程序,我试了几个数,好象对,弄不准,望指点,先谢谢呀!!!

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

public class Hlt extends Applet{
Label numLabel;
TextField num;
int yPosition;

public void init()
{
numLabel = new Label ("Enter an integer:");
num = new TextField(10);

add(numLabel);
add(num);

}

public boolean action (Event e,Object o )
{
long number;
number =Long.parseLong( num.getText());

ac(number);
return true;

}


void ab(long n)
{
if (n==1)
System.out.println("1:A->B\n");
else
{
ac(n-1);
System.out.println(n+":A->B") ;
cb(n-1);
}

}
void ac(long n)
{
if (n==1)
System.out.println("1:A->C\n");
else
{
ab(n-1);
System.out.println(n+":A->C") ;
bc(n-1);
}

}
void bc(long n)
{
if (n==1)
System.out.println("1:B->C\n");
else
{
ba(n-1);
System.out.println(n+":B->C") ;
ac(n-1);
}

}
void ba(long n)
{
if (n==1)
System.out.println("1:B->A\n");
else
{
bc(n-1);
System.out.println(n+":B->A") ;
ca(n-1);
}

}
void ca(long n)
{
if (n==1)
System.out.println("1:C->A\n");
else
{
cb(n-1);
System.out.println(n+":C->A") ;
ba(n-1);
}

}
void cb(long n)
{
if (n==1)
System.out.println("1:C->B\n");
else
{
ca(n-1);
System.out.println(n+":C->B") ;
ab(n-1);
}

}

}


   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