第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的整數(shù)增加了 2 而不是 1?

為什么我的整數(shù)增加了 2 而不是 1?

Helenr 2022-10-20 17:10:19
我正在為家庭作業(yè)做河內(nèi)塔作業(yè)。我試圖將 i 變量每次增加 1,但它增加了 2。此外, ("[g]et, [p]ut... 字符串被打印兩次,而不是一次。發(fā)生了什么?!請幫忙!>我嘗試添加一個 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;                    }                }            }        }    } }作為回答者的請求,我發(fā)布了整個代碼。
查看完整描述

2 回答

?
開滿天機

TA貢獻1786條經(jīng)驗 獲得超13個贊

("[g]et, [p]ut... 字符串被打印兩次,因為在您輸入并按 Enter 后,for 循環(huán)會為您輸入的輸入運行一次,再為“enter”運行一次輸入后按下按鈕


根據(jù)你想要的,在 else if (choice.equals("p")) this elsed if 塊中減少 i


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

//your code

i--;

}


查看完整回答
反對 回復 2022-10-20
?
慕沐林林

TA貢獻2016條經(jīng)驗 獲得超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 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號