?? 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 + -