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

标签 java loops integer bluej

我正在做汉诺塔作业作为家庭作业。我试图每次将 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; 
                }
            }
        }
    } 
}

我发布了整个代码,作为回答者的请求。

最佳答案

("[g]et, [p]ut... 字符串被打印两次,因为在您给出输入并按 Enter 键后,for 循环将针对您给出的输入运行一次,并再运行一次输入后按下“enter”按钮

根据你想要的,在 else if (choice.equals("p")) 这个 elsed if block 中减少 i

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

关于java - 为什么我的整数增加了 2 而不是 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55841252/

相关文章:

java - 通过套接字发送对象java

java - Java 中的异常处理 - Final

javascript - 可以将数字存储为 float 吗?

java - 短信如何存储在手机中?

java - 无法在与 findViewById(R.id.textView) 同一行中使用 setText() 方法

javascript - JS : encapsulated foreach loop with arrays for JSON

javascript - 在javascript中循环和存储不同长度的obj

Javascript 打印多个网页 iFrame 内容(打印所有按钮)

java - int 和 Integer 的区别

javascript - javascript JSON 对象中是否存在整数和字符串键的排序?