【问题标题】:Why is my integer going up by 2 instead of 1?为什么我的整数增加了 2 而不是 1?
【发布时间】:2019-09-14 09:53:13
【问题描述】:

我正在做河内塔的家庭作业。我试图将 i 变量每次增加 1,但它增加 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; 
                }
            }
        }
    } 
}

应答复者的要求,我发布了整个代码。

【问题讨论】:

  • 你为什么要i--
  • i 应该算什么?为什么只在i 计数到length 之前一直询问用户输入?
  • 我想你可能需要告诉我们什么叫这个,如果有的话。
  • @DawoodibnKareem 我这样做了——试图让字符串不会重复。
  • @KevinAnderson 我正在计算塔上的磁盘数量。当塔填满时它会停止。

标签: java loops integer bluej


【解决方案1】:

("[g]et, [p]ut... 字符串被打印了两次,因为在您输入并按回车后,for 循环会为您输入的输入运行一次,然后再运行一次输入后按下“输入”按钮

根据你想要的,在 else if (choice.equals("p")) this elsed if 块中减 i

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

【讨论】:

  • 我没有工作。它仍然出现了两次。 else if (choice.equals("p")) { System.out.println("Disk?");大小 = input.nextInt();塔[i] = 大小;塔计数器 = 0; System.out.print("{");一世 - ; while (towerCounter
  • 替换这一行choice = input.nextLine();选择 = input.next(); anre 删除 i-- 在choice.equals("p")
【解决方案2】:

我推荐使用递归,“公式”更容易: 代码如下:

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

【讨论】:

  • 这实际上是一个了不起的想法,但我们的老师想要这样的格式。
  • if (choice.equals("g")) { // TODO: 从塔中取出一个磁盘 } // 把 else if (choice.equals("p")) { // TODO : 在塔上放一个磁盘 } // 退出 else if (choice.equals("e")) { playing = false; }
  • TODO 应该填空。
  • 当然,我改变了问题并添加了整个代码。
猜你喜欢
  • 2018-09-30
  • 2013-08-12
  • 2020-10-04
  • 1970-01-01
  • 2011-05-12
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多