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

时间:2019-04-25 03:08:02

标签: java loops integer bluej

我正在做河内作业塔。我试图将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; 
                }
            }
        }
    } 
}

我应答者的要求张贴了整个代码。

2 个答案:

答案 0 :(得分:1)

(“ [g] et,[p] ut ...字符串被打印两次,因为在您输入并按下回车后,for循环对于您输入的内容运行一次,而对于输入后按下“输入”按钮

根据您想要的内容,在else if(choice.equals(“ p”))中将i减1,否则将i减

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

答案 1 :(得分:0)

我建议使用 递归 ,“公式”要简单得多: 这是代码:

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);
    }
  }
}