如果声明在河内塔解决方案中无法正常工作

时间:2015-03-09 14:31:01

标签: java if-statement recursion

在此代码中找到河内塔问题的解决方案我将N(磁盘数量)设置为4,作为示例,当我运行时打印出来:

import java.util.Scanner;
public class HanoiTower {

  public static void main(String[] args) {
    int N;
    Scanner in=new Scanner(System.in);
    System.out.println("Choose number of disks? ");
    N= in.nextInt();
    in.close();
    towers(N,0,1,2);        
  }

  static void towers(int disks, int a,int b, int spare){ 

    if (disks==1){
      System.out.println("Move disk 1 from 1 to 2 ");
    }
    else{
      towers(disks-1, a,spare,b);
      System.out.printf("Move disk %d from stack %d to stack %d ", disks, a, b);
      towers(disks-1,spare,b,a);
    }
  }
}

我怀疑为什么它首先打印if语句?

 Move disk 1 from 1 to 2 
 Move disk 2 from stack 0 to stack 2 Move disk 1 from 1 to 2 
 Move disk 3 from stack 0 to stack 1 Move disk 1 from 1 to 2 
 Move disk 2 from stack 2 to stack 1 Move disk 1 from 1 to 2 
 Move disk 4 from stack 0 to stack 2 Move disk 1 from 1 to 2 
 Move disk 2 from stack 1 to stack 0 Move disk 1 from 1 to 2 
 Move disk 3 from stack 1 to stack 2 Move disk 1 from 1 to 2 
 Move disk 2 from stack 0 to stack 2 Move disk 1 from 1 to 2 
 Move disk 5 from stack 0 to stack 1 Move disk 1 from 1 to 2 
 Move disk 2 from stack 2 to stack 1 Move disk 1 from 1 to 2 
 Move disk 3 from stack 2 to stack 0 Move disk 1 from 1 to 2 
 Move disk 2 from stack 1 to stack 0 Move disk 1 from 1 to 2 
 Move disk 4 from stack 2 to stack 1 Move disk 1 from 1 to 2 
 Move disk 2 from stack 0 to stack 2 Move disk 1 from 1 to 2 
 Move disk 3 from stack 0 to stack 1 Move disk 1 from 1 to 2 
 Move disk 2 from stack 2 to stack 1 Move disk 1 from 1 to 2 

3 个答案:

答案 0 :(得分:1)

您的方法towers会递归,直到disks等于1.您可以在开头记录磁盘的值,如

static void towers(int disks, int a, int b, int spare) {
    System.out.printf("Disks = %d%n", disks);
    if (disks == 1) {
        System.out.println("Move disk 1 from 1 to 2 ");
    } else {
        towers(disks - 1, a, spare, b);
        System.out.printf("Move disk %d from stack %d to stack %d ", disks,
                a, b);
        towers(disks - 1, spare, b, a);
    }
}

你可以看到为什么你得到你的输出。另外,关闭Scanner周围的System.in是个坏主意(一旦关闭System.in,就无法再次打开它。)

答案 1 :(得分:1)

它始终首先进入else块并按照您的设计进行递归。所以带有一些打印语句的输出会澄清它......

public static void main(String[] args){

    int N;
    Scanner in=new Scanner(System.in);
    System.out.println("Choose number of disks? ");
    N= in.nextInt();
    in.close();
    System.out.println("Printing the N..."+N);
    towers(N,0,1,2);

}

static void towers(int disks, int a,int b, int spare){
    System.out.println("Printing the disks..."+disks);

    if (disks==1){
        System.out.println("Move disk 1 from 1 to 2 ");}
    else{
        towers(disks-1, a,spare,b);
        System.out.printf("Move disk %d from stack %d to stack %d ", disks, a, b);
        towers(disks-1,spare,b,a);
    }
    }

输出:

Printing the N...4
Printing the disks...4
Printing the disks...3
Printing the disks...2
Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 2 from stack 0 to stack 1 Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 3 from stack 0 to stack 2 Printing the disks...2
Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 2 from stack 1 to stack 2 Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 4 from stack 0 to stack 1 Printing the disks...3
Printing the disks...2
Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 2 from stack 2 to stack 0 Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 3 from stack 2 to stack 1 Printing the disks...2
Printing the disks...1
Move disk 1 from 1 to 2 
Move disk 2 from stack 0 to stack 1 Printing the disks...1
Move disk 1 from 1 to 2 

答案 2 :(得分:0)

if正常工作,你可以肯定它。如果您使用任何大于1的数字调用该方法,它将在行towers(disks-1, a,spare,b);中递归调用自身,直到该数字为1.然后它将打印第一条消息。