十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
public class Hannoi {
创新互联专注于安远网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供安远营销型网站建设,安远网站制作、安远网页设计、安远网站官网定制、小程序开发服务,打造安远网络公司原创品牌,更为您提供安远网站排名全网营销落地服务。
public static void main(String[] args) {
int nDisks = 3;
moveDish(nDisks, 'A', 'B', 'C');
}
private static void moveDish(int level, char from, char inter, char to) {
if (level == 1) {
System.out.println("从" + from + "移动盘子 " + level + " 号到" + to);
} else {
moveDish(level - 1, from, to, inter);
System.out.println("从" + from + "移动盘子 " + level + " 号到" + to);
moveDish(level - 1, inter, from, to);
}
}
}
定义了服务器线程类,服务器运行在一个单独的线程中。
客户端运行在主线程中。
所有代码放在一个源文件中就行。源文件名是Hanoi.java
下面是源代码,输入的盘子数不要太大,20以内,否则会步数太多,输出耗时太久。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Hanoi {
public static void main(String[] args) {
//创建服务器
HanoiServer server = new HanoiServer();
server.start();//启动服务器
/*开始创建客户端*/
Socket socket = null;//客户端Socket
try {
socket = new Socket(InetAddress.getLocalHost(), 8888);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream printStream = new PrintStream(socket.getOutputStream());
Scanner scanner=new Scanner(System.in);
System.out.println("请输入盘子数(3-10),数字太大,运算时间就会太长可能会卡死。");
printStream.print(scanner.nextInt());
printStream.println();
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
System.out.println("客户端socket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class HanoiServer extends Thread {//服务器线程类
private ServerSocket serverSocket;
public HanoiServer() {
try {
this.serverSocket = new ServerSocket(8888);
} catch (IOException e) {
e.printStackTrace();
}
}
private void hanoi(int n, String from, String inter, String to, PrintStream printStream) {
if (n == 1) {
printStream.print("Disk 1 from " + from + " to " + to);
printStream.println();
} else {
hanoi(n - 1, from, to, inter, printStream);
printStream.print("Disk " + n + " from " + from + " to " + to);
printStream.println();
hanoi(n - 1, inter, from, to, printStream);
}
}
@Override
public void run() {
Socket socket = null;
try {
socket = this.serverSocket.accept();
PrintStream printStream = new PrintStream(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int n = Integer.parseInt(reader.readLine());
this.hanoi(n, "A", "B", "C", printStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
System.out.println("服务器socket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
try {
this.serverSocket.close();
System.out.println("服务器serverSocket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这样应该可以了
如果还有那个地方不懂的,建议你研究下汉诺塔算法
import
java.io.BufferedReader;//引入IO包中的BufferedReader
import
java.io.IOException;//引入IO包中的IO异常处理
import
java.io.InputStreamReader;//引入IO包中的InputStreaReader
public
class
Hinoi
{
//主类
static
int
m=0;//定义移动的次数
//主程序入口--main方法
public
static
void
main(String[]
args)
{
//创建BufferedReader对象,InputStream输入流
BufferedReader
bf
=
new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("请输入盘子的个数:");
try
{
int
sl
=
Integer.parseInt(bf.readLine().toString());//接收总盘子个数
toMove(sl,"A","B","C");//调用移动方法
A--C
}
catch
(NumberFormatException
e)
{捕获NumberFormatException异常
//
TODO
Auto-generated
catch
block
e.printStackTrace();//打印异常
}
catch
(IOException
e)
{//捕获IOException异常
//
TODO
Auto-generated
catch
block
e.printStackTrace();//打印异常
}
System.out.println("总共移动了:"+m+"
次数");//打印移动次数
}
//移动方法
private
static
void
toMove(int
sl,
String
one,
String
two,String
three)
{
if(sl==1){//如果只有一个盘子,则直接移动到C柱
System.out.println("盘子"+sl+"
从
"+one+"----"+three);
}else{//如果总盘数大于1,则递归调用移动方法
//把所有的数量为sl-1的盘子全部从A移到到B(C作为一个过渡),好提供一个最下面的位置给最大盘子到C;
toMove(sl-1,one,three,two);
System.out.println("盘子"+sl+"
从
"+one+"----"+three);
//把所有的剩余的盘子从B移动到C(A作为一个过渡)
toMove(sl-1,two,one,three);
}
m++;
}
}
源代码:
/**
*本程序完成的功能是利用汉递规算法实现汉诺塔的动态演示程序
*/
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.*;
public class Hanio extends JApplet implements ActionListener, Runnable
{
/**
*diskNum是盘子的数量
*/
private int diskNum ;
/**
*各个组件的句柄
*/
private JButton begin, stop;
private JLabel lDiskNum;
private JTextField text;
JPanel pane;
/**
*定义一个线程句柄
*/
private Thread animate;
/**
*定义a,b,c三个柱子上是否有盘子,有哪些盘子
*/
private int adisk[];
private int bdisk[];
private int cdisk[];
public void init()
{
Container content = getContentPane();
content.setLayout(new BorderLayout());
lDiskNum = new JLabel(盘子的数目);
text = new JTextField(8);
begin = new JButton(开始);
begin.addActionListener(this);
stop = new JButton(停止);
stop.addActionListener(this);
pane = new JPanel();
pane.setLayout(new FlowLayout());
pane.add(lDiskNum);
pane.add(text);
pane.add(begin);
pane.add(stop);
content.add(pane, BorderLayout.SOUTH);
}
public void paint(Graphics g)
{
Graphics2D g2D = (Graphics2D)g;
Ellipse2D.Double ellipse;
g2D.setPaint(getBackground());
if(adisk != null)
{
/**
*消除以前画的盘子
*/
for(int j=adisk.length, i=0; --j=0; i++ )
{
ellipse = new Ellipse2D.Double(20+i*5, 180-i*10, 180-i*10, 20);
g2D.fill(ellipse);
ellipse = new Ellipse2D.Double(220+i*5, 180-i*10, 180-i*10, 20);
g2D.fill(ellipse);
ellipse = new Ellipse2D.Double(420+i*5, 180-i*10, 180-i*10, 20);
g2D.fill(ellipse);
}
drawEllipse(g, 20, adisk);//画A组盘子
drawEllipse(g, 220, bdisk);//画B组盘子
drawEllipse(g, 420, cdisk);//画C组盘子
}
pane.repaint();
}
public void update(Graphics g)
{
paint(g);
}
/**画出椭圆代表盘子,g是图形环境,x是最下面的盘子的横坐标,
*arr是柱子数组
*/
public void drawEllipse(Graphics g,int x,int arr[])
{
Graphics2D g2D = (Graphics2D)g;
Ellipse2D.Double ellipse;
g2D.setPaint(Color.gray);
g2D.draw(new Line2D.Double(x+90, 10, x+90, 180));
for(int j=arr.length, i=0; --j=0; i++ )
if(arr[j] != 0)
{
if(i%2 == 0)
g2D.setPaint(Color.blue);
else
g2D.setPaint(Color.red);
ellipse = new Ellipse2D.Double(x+i*5, 180-i*10, 180-i*10, 20);
g2D.fill(ellipse);
}
}
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if(command.equals(开始))
{
/**
*进行初始化,开始的时候只有a柱子上有盘子,其他柱子都没有
*/
diskNum = Integer.parseInt(text.getText());
adisk = new int[diskNum];
for(int i=0; iadisk.length; i++)
adisk[i] = 1;
bdisk = new int[diskNum];
for(int k=0; kbdisk.length; k++)
bdisk[k] = 0;
cdisk = new int[diskNum];
for(int i=0; icdisk.length; i++)
cdisk[i] = 0;
repaint();
if(animate == null || !animate.isAlive())//创建一个线程
{
animate = new Thread(this);
animate.start();
}
}
if(command.equals(停止))
{
for(int k=0; kbdisk.length; k++)
bdisk[k] = 0;
for(int i=0; icdisk.length; i++)
cdisk[i] = 0;
repaint();
text.setText();
animate = null;
}
}
/**
*线程方法,在此调用汉诺塔执行移动盘子操作
*/
public void run()
{
hanio(diskNum, 'A', 'B', 'C');
repaint();
}
/**
*汉诺塔递规调用程序,n是盘子的数量,A,B,C分别代表三个柱子
*/
public void hanio(int n, char A, char B, char C)
{
if(n 1)
{
hanio(n-1, A, C, B);
pause();//停顿几秒在执行
switch(A)
{
case 'A':adisk[n-1] = 0;break;
case 'B':bdisk[n-1] = 0;break;
case 'C':cdisk[n-1] = 0;break;
default:break;
}
switch(C)
{
case 'A':adisk[n-1] = 1;break;
case 'B':bdisk[n-1] = 1;break;
case 'C':cdisk[n-1] = 1;break;
default:break;
}
repaint();
hanio(n-1, B, A, C);
}
pause();
switch(A)
{
case 'A':adisk[n-1] = 0;break;
case 'B':bdisk[n-1] = 0;break;
case 'C':cdisk[n-1] = 0;break;
default:break;
}
switch(C)
{
case 'A':adisk[n-1] = 1;break;
case 'B':bdisk[n-1] = 1;break;
case 'C':cdisk[n-1] = 1;break;
default:break;
}
repaint();
}
/**
*每隔半妙钟移动一个盘子
*/
public void pause()
{
try{
Thread.sleep(500);//可以修改此值加快盘子移动的速度
}catch(InterruptedException e){}
}
}