新手上路,整了个汉诺塔的小程序,我试了几个数,好象对,弄不准,望指点,先谢谢呀!!!
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);
}
}
}