为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Helenr 2022-10-20 17:10:19
我正在为家庭作业做河内塔作业。我试图将 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;                    }                }            }        }    } }作为回答者的请求,我发布了整个代码。
查看完整描述

2 回答

?
开满天机

TA贡献1786条经验 获得超13个赞

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


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


 else if (choice.equals("p")){

//your code

i--;

}


查看完整回答
反对 回复 2022-10-20
?
慕沐林林

TA贡献2016条经验 获得超9个赞

我建议使用Recursion,“公式”要容易得多:这是代码:


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

    }

  }

}


查看完整回答
反对 回复 2022-10-20
  • 2 回答
  • 0 关注
  • 94 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号