📄 hanoi.java
字号:
import java.io.*;
public class hanoi
{
static void hanoi(int n, String startpeg, String middlepeg, String endpeg)
{
if (n == 1)
System.out.println("move " + startpeg + " to " + endpeg);
else {
hanoi(n - 1, startpeg, endpeg, middlepeg);
System.out.println("move " + startpeg + " to " + endpeg);
hanoi(n - 1, middlepeg, startpeg, endpeg);
}
}
public static void main(String[] args)throws IOException
{
int n = 0;
char ans;
while(true)
{
System.out.print("Please enter the number of the discs: ");
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(input.readLine());
String startpeg = "A";
String middlepeg = "B";
String endpeg = "C";
System.out.println("The solution for n = " + n);
hanoi(n, startpeg, middlepeg, endpeg);
System.out.println("Do u want to try again?(y or n");
ans = (char)input.read();
if(ans!='y'&&ans!='Y')
{
System.out.println("Thank you for using it, goodbye!");
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -