为什么我的整数增加了 2 而不是 1?

Why is my integer going up by 2 instead of 1?

我正在做汉诺塔的家庭作业。我试图每次将 i 变量增加一个,但它增加了 2。此外, ("[g]et, [p]ut... 字符串被打印了两次,而不是一次。发生了什么?!请帮忙!>

我正在尝试添加一个 i--;在 if (p) 上,但这没有用。

import java.util.Scanner;

/**
 * Simulates a tower that can hold disks.
 * @author S. Camilleri
 * @author <your name>
 */
public class TowersOfHanoi {
    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in);

        // This array holds the disks. A 0 represents no disk.
        int[] tower = new int[5];

        // This index represents the first available empty spot for a disk.
        int index = 0;

        int towerCounter = 0;
        int length = tower.length;

        boolean playing = true;    
        while (playing)
        {
            /********************
             * Display the tower
             ********************/
            System.out.println();
            System.out.print("{ ");

            while (towerCounter < length) {
                tower[towerCounter] = 0;
                System.out.print(tower[towerCounter]);
                towerCounter = towerCounter + 1;
            }
            String choice;
            int size;
            for (int i=0; i<length; i++) {

                /********************
                 * Get action from user
                 ********************/      
                System.out.println();      
                System.out.println("[g]et, [p]ut or [e]xit?");
                choice = input.nextLine();

                // Get
                if (choice.equals("g"))
                {
                    tower[i] = 0;

                    System.out.println();

                    towerCounter = 0;
                    i--;
                    System.out.print("{ ");

                    while (towerCounter < length) {

                        System.out.print(tower[towerCounter]);
                        towerCounter = towerCounter + 1;
                    }

                }

                // Put
                else if (choice.equals("p"))
                {
                    System.out.println("Disk?");
                    size = input.nextInt();
                    tower[i] = size;
                    towerCounter = 0;


                    System.out.print("{ ");
                    while (towerCounter < length) {

                        System.out.print(tower[towerCounter]);
                        towerCounter = towerCounter + 1;
                    }
                }

                // Exit
                else if (choice.equals("e"))
                {
                    playing = false; 
                }
            }
        }
    } 
}

应回答者的要求,我贴出了完整的代码。

我推荐使用递归,"formula"更简单: 这是代码:

public class TowerOf_Hanoi {
public static void main(String [] args){
    java.util.Scanner input=new java.util.Scanner(System.in);

    System.out.print("Enter Number of Disks:   ");
    int numDisk=input.nextInt();

    System.out.println("Moves are: ");
    steps(numDisk,'A','B','C');

}

public static void steps(int n, char fromTower, char toTower, char auxTower){

    //base case for Recursion
    if(n==1)        //if n=1 it will stop
        System.out.println("Move disk "+n+" from "+fromTower+" to "+toTower);
    else{
        steps(n-1,fromTower,auxTower,toTower);      //recursion
        System.out.println("Move disk "+n+" from "+fromTower+" to "+toTower);
        steps(n-1,auxTower,toTower,fromTower);
    }
  }
}

("[g]et, [p]ut... 字符串被打印两次,因为在您输入并按下回车后,for 循环是 运行 一次用于您提供的输入并且输入

后再次按下 "enter" 按钮

根据你的需要,在 else if (choice.equals("p")) else if block also

中递减 i
 else if (choice.equals("p")){
//your code
i--;
}