定义了服务器线程类,服务器运行在一个单独的线程中。
成都创新互联公司是专业的广东网站建设公司,广东接单;提供成都网站设计、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行广东网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
客户端运行在主线程中。
所有代码放在一个源文件中就行。源文件名是Hanoi.java
下面是源代码,输入的盘子数不要太大,20以内,否则会步数太多,输出耗时太久。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java点虐 .InetAddress;
import java点虐 .ServerSocket;
import java点虐 .Socket;
import java点虐 .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();
}
}
}
}
package Hanoi;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Hanoi {
public static void main(String args[]) throws IOException {
Hanoi aa = new Hanoi();
aa.go();
}
public void go() throws IOException {
int n;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入盘数:");
n = Integer.parseInt(buf.readLine());
Hanoi hanoi = new Hanoi();
hanoi.move(n, 'A', 'B', 'C');
}
public void move(int n, char a, char b, char c) {
if (n == 1) {
System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
} else {
move(n - 1, a, c, b);
System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
move(n - 1, b, a, c);
}
}
}
这样应该可以了
如果还有那个地方不懂的,建议你研究下汉诺塔算法
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++;
}
}