亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

英文標尺遞歸問題

英文標尺遞歸問題

PIPIONE 2023-09-06 15:41:00
我正在嘗試試運行書中的英語標尺方法,但是試運行有點令人困惑,而且我的輸出和原始輸出不同。public static void drawRuler(int nInches, int majorlength){  drawLine(majorLength, 0);  for (int j =1; j<= nInches, j++)  {   drawInterval(majorLength-1);   drawLine(majorLength, j); }}private static void drawInterval (int centralLength){ if (centralLength>=1) {  drawInterval(centralLength - 1);  drawLine(centralLength);  drawInterval(centrakLength-1); }}public static void drawLine(int tickLength, int tickLabel){ for (int j=0; j<ticklength; j++)   System.out.print("-") if (tickLabel>=0)  System.out.print(" "+tickLable); System.out.print("\n");}private static void drawLine(int tickLength){  drawLine(tickLength, -1);}第一次,我進入nInches = 1 and majorlength 3,1- 將使用 (3,0) // 刻度長度和刻度標簽調用drawLinepublic static void drawLine(3, 0)    {     for (int j=0; j<3; j++)       System.out.print("-")     if (tickLabel>=0)      System.out.print(" "+tickLable);     System.out.print("\n");    }輸出:--- 02-現在下面的循環將從drawRuler函數運行for (int j =1; j<=1, j++)      {       drawInterval(majorLength-1);* Point1: 意味著上面的線會調用drawInterval(2) ?*drawLine(majorLength, j);     }3-我們轉到以 2 作為參數的函數drawIntervalprivate static void drawInterval (2)    {     if (centralLength>=1)   // true     {      drawInterval(centralLength - 1); **Point 2: what the point of calling same function with 1 ? and will it call itself again without drawing anything? and function will go on nextline after drawInterval become 0?**drawLine(centralLength);      drawInterval(centrakLength-1);     }    }Point3:drawLine(tickLength, -1);為什么我們用這個-1?
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊


你的代碼甚至無法編譯,有很多語法錯誤和變量用詞不當。您是否通過掃描 + OCR 從書中獲取了源代碼,而沒有嘗試使用 Java 運行它?

因此,首先讓我們修復錯誤,并將這段零散的代碼變成一個帶有 main 方法的類,好嗎?

package de.scrum_master.stackoverflow;


public class EnglishRuler {

? public static void main(String[] args) {

? ? drawRuler(2, 4);

? }


? public static void drawRuler(int nInches, int majorLength) {

? ? drawLine(majorLength, 0);

? ? for (int j = 1; j <= nInches; j++) {

? ? ? drawInterval(majorLength - 1);

? ? ? drawLine(majorLength, j);

? ? }

? }


? private static void drawInterval(int centralLength) {

? ? if (centralLength >= 1) {

? ? ? drawInterval(centralLength - 1);

? ? ? drawLine(centralLength);

? ? ? drawInterval(centralLength - 1);

? ? }

? }


? private static void drawLine(int tickLength, int tickLabel) {

? ? for (int j = 0; j < tickLength; j++)

? ? ? System.out.print("-");

? ? if (tickLabel >= 0)

? ? ? System.out.print(" " + tickLabel);

? ? System.out.print("\n");

? }


? private static void drawLine(int tickLength) {

? ? drawLine(tickLength, -1);

? }

}

drawRuler(1, 3)印刷:


--- 0

-

--

-

--- 1

drawRuler(1, 5)印刷:


----- 0

-

--

-

---

-

--

-

----

-

--

-

---

-

--

-

----- 1

drawRuler(2, 4)印刷:


---- 0

-

--

-

---

-

--

-

---- 1

-

--

-

---

-

--

-

---- 2

這一切都在預料之中?,F在讓我們向程序添加一些可選的調試輸出:


package de.scrum_master.stackoverflow;


public class EnglishRuler {

? private static boolean DEBUG = true;

? private static String indent = "";


? public static void main(String[] args) {

? ? drawRuler(1, 3);

? }


? public static void drawRuler(int nInches, int majorLength) {

? ? if (DEBUG)

? ? ? System.out.println("drawRuler(" + nInches + ", " + majorLength + ")");

? ? drawLine(majorLength, 0);

? ? for (int j = 1; j <= nInches; j++) {

? ? ? drawInterval(majorLength - 1);

? ? ? drawLine(majorLength, j);

? ? }

? }


? private static void drawInterval(int centralLength) {

? ? indent += "? ";

? ? if (DEBUG)

? ? ? System.out.println(indent + "drawInterval(" + centralLength + ")");

? ? if (centralLength >= 1) {

? ? ? drawInterval(centralLength - 1);

? ? ? drawLine(centralLength);

? ? ? drawInterval(centralLength - 1);

? ? }

? ? indent = indent.substring(2);

? }


? private static void drawLine(int tickLength, int tickLabel) {

? ? indent += "? ";

? ? if (DEBUG)

? ? ? System.out.println(indent + "drawLine(" + tickLength + ", " + tickLabel + ")");

? ? for (int j = 0; j < tickLength; j++)

? ? ? System.out.print("-");

? ? if (tickLabel >= 0)

? ? ? System.out.print(" " + tickLabel);

? ? System.out.print("\n");

? ? indent = indent.substring(2);

? }


? private static void drawLine(int tickLength) {

? ? drawLine(tickLength, -1);

? }

}

DEBUG只要是,這就不會改變輸出false。如果將其設置為true,則日志將drawRuler(1, 3)變為:


drawRuler(1, 3)

? drawLine(3, 0)

--- 0

? drawInterval(2)

? ? drawInterval(1)

? ? ? drawInterval(0)

? ? ? drawLine(1, -1)

-

? ? ? drawInterval(0)

? ? drawLine(2, -1)

--

? ? drawInterval(1)

? ? ? drawInterval(0)

? ? ? drawLine(1, -1)

-

? ? ? drawInterval(0)

? drawLine(3, 1)

--- 1

在那里你有一個自動生成的試運行版本。


至于你的問題:


第一次,我進入nInches = 1并且majorlength = 3,


1)drawLine將用(3,0)(tickLength和tickLabel)調用


正確的。


Point1:上面那行的意思是會調用drawInterval(2)?


正確的。


第3點:drawLine(tickLength, -1). 我們為什么用這個-1?


因為里面drawLine(int tickLength, int tickLabel)說:


? ? if (tickLabel >= 0)

? ? ? System.out.print(" " + tickLabel);

因此,使值tickLabel小于零只是當我們不在主間隔而是在其間較小的子間隔時避免打印標簽的一種方法。


更新:我還根據遞歸級別向具有調試輸出的程序版本添加了縮進,并且還更新了日志輸出以縮進,以便OP更好地理解。


更新 2:您可以通過內聯便捷方法來簡化程序,drawLine(int tickLength)如下所示:


? private static void drawInterval(int centralLength) {

? ? // ...

? ? ? drawInterval(centralLength - 1);

? ? ? drawLine(centralLength, -1);? // Note the additional ", -1"

? ? ? drawInterval(centralLength - 1);

? ? // ...

? }

然后刪除這個,因為現在它不再使用了:


? // Delete me!

? private static void drawLine(int tickLength) {

? ? drawLine(tickLength, -1);

? }

更新 3:因為您似乎對我沒有為方便方法打印日志輸出感到非常惱火drawLine(int tickLength),所以這里也是為該方法生成輸出的原始程序的另一個擴展版本,現在完全復制您的筆和紙試運行:


package de.scrum_master.stackoverflow;


public class EnglishRuler {

? private static boolean DEBUG = true;

? private static String indentText = "";


? public static void main(String[] args) {

? ? drawRuler(1, 3);

? }


? public static void drawRuler(int nInches, int majorLength) {

? ? debugPrint("drawRuler(" + nInches + ", " + majorLength + ")");

? ? drawLine(majorLength, 0);

? ? for (int j = 1; j <= nInches; j++) {

? ? ? drawInterval(majorLength - 1);

? ? ? drawLine(majorLength, j);

? ? }

? }


? private static void drawInterval(int centralLength) {

? ? indent();

? ? debugPrint("drawInterval(" + centralLength + ")");

? ? if (centralLength >= 1) {

? ? ? drawInterval(centralLength - 1);

? ? ? drawLine(centralLength);

? ? ? drawInterval(centralLength - 1);

? ? }

? ? dedent();

? }


? private static void drawLine(int tickLength, int tickLabel) {

? ? indent();

? ? debugPrint("drawLine(" + tickLength + ", " + tickLabel + ")");

? ? for (int j = 0; j < tickLength; j++)

? ? ? System.out.print("-");

? ? if (tickLabel >= 0)

? ? ? System.out.print(" " + tickLabel);

? ? System.out.print("\n");

? ? dedent();

? }


? private static void drawLine(int tickLength) {

? ? indent();

? ? debugPrint("drawLine(" + tickLength + ")");

? ? drawLine(tickLength, -1);

? ? dedent();

? }


? private static void debugPrint(String message) {

? ? if (DEBUG)

? ? ? System.out.println(indentText + message);

? }


? private static void indent() {

? ? indentText += "? ";

? }


? private static void dedent() {

? ? indentText = indentText.substring(2);

? }

}

更新后的控制臺日志變為:


drawRuler(1, 3)

? drawLine(3, 0)

--- 0

? drawInterval(2)

? ? drawInterval(1)

? ? ? drawInterval(0)

? ? ? drawLine(1)

? ? ? ? drawLine(1, -1)

-

? ? ? drawInterval(0)

? ? drawLine(2)

? ? ? drawLine(2, -1)

--

? ? drawInterval(1)

? ? ? drawInterval(0)

? ? ? drawLine(1)

? ? ? ? drawLine(1, -1)

-

? ? ? drawInterval(0)

? drawLine(3, 1)

--- 1

我發現這是不必要的,但如果它對你有幫助,我很高興。


查看完整回答
反對 回復 2023-09-06
  • 1 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號