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();
}
}
}
}
【汉诺塔代码java 汉诺塔代码Java】汉诺塔代码java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于汉诺塔代码Java、汉诺塔代码java的信息别忘了在本站进行查找喔 。
推荐阅读
- java代码实现自动执行,java代码实现自动执行程序
- 民用gis精度,gis测量距离
- 酒水直播卖场话术,酒水直播卖场话术怎么说
- c语言设计分段函数 c语言分段函数编写
- 什么是睡眠视频,睡眠视频素材
- sap软件实施,sap系统实施
- python爬虫selector,python爬虫selenium当没有数据就跳过
- php实现数据递增查询 php数字递增
- python3字典数量,python中字典的用法