我正在嘗試使用遞回列印星形圖案。
問題是,當我的圖案完全列印出來時,它會在一行或兩行間隙后遞回地再次開始列印圖案。
看來我的基本情況是正確的,我在哪里做錯了?
我的代碼:
public static void printRecursion(int row) {
int i = 1;
printRecursion2(row, i);
}
private static void printRecursion2(int row, int i) {
if (row <= 0) {
return;
}
if (i <= row) {
System.out.print("* ");
i ;
printRecursion2(row, i);
}
System.out.println();
i = 1;
printRecursion2(row - 1, i);
}
我得到的輸出:
* * * *
* * *
* *
*
*
*
* *
*
*
*
* *
*
*
*
* *
*
*
*
* * *
* *
*
*
*
* *
*
*
*
* *
*
*
*
* *
*
*
*
* * *
* *
*
*
*
* *
*
*
*
* *
*
*
*
* *
*
*
*
* * *
* *
*
*
*
* *
*
*
*
* *
*
*
*
* *
*
*
*
* * *
* *
*
*
*
* *
*
*
*
* *
*
*
*
* *
*
*
*
我想要的輸出:
* * * *
* * *
* *
*
uj5u.com熱心網友回復:
您正在嘗試使用相同的遞回方法來列印單行并列印所有行。它應該分為兩種遞回方法,即一種用于列印單行,另一種用于列印所有行。
在下面的代碼中,方法printRow以遞回方式列印單行,而方法以遞回方式printRecursion列印所有行。
public class Main {
private static void printRow(int length) {
if (length == 0) {
System.out.println();
}
else {
System.out.print("* ");
printRow(length - 1);
}
}
private static void printRecursion(int row) {
if (row <= 0) {
return;
}
printRow(row);
printRecursion(row - 1);
}
public static void main(String[] args) {
printRecursion(3);
}
}
我得到的輸出是:
* * *
* *
*
為了了解您的代碼失敗的原因,您應該使用除錯器運行它。在每次遞回呼叫 之后printRecursion2,在if(i<=row)該方法中,該方法的其余部分被執行。列印單行后,您嘗試繼續遞回,但要針對所有行。
以下代碼試圖演示您的代碼在做什么。當進行遞回呼叫時,我不列印*(星號),而是增加一個iteration變數并列印它。方法終止后,我遞減iteration并列印它。這是代碼:
public class Main {
private static int iteration = 0;
public static void printRecursion(int row) {
int i=1;
printRecursion2(row,i);
}
private static void printRecursion2(int row,int i) {
iteration;
debug();
if(row<=0)
{
return;
}
if(i<=row)
{
i ;
printRecursion2(row,i);
--iteration;
debug();
}
System.out.println();
i=1;
printRecursion2(row-1,i);
--iteration;
debug();
}
private static void debug() {
for (int i = 0; i < iteration; i ) {
System.out.print(" ");
}
System.out.println("Iteration: " iteration);
}
public static void main(String[] args) {
printRecursion(3);
}
}
這是輸出:(
我在前幾行添加了注釋,希望它可以幫助您可視化代碼流。)
Iteration: 1 <- called from 'printRecursion'
Iteration: 2 <- prints first * in first row
Iteration: 3 <- prints second * in first row
Iteration: 4 <- prints third * in first row
Iteration: 5 <- 'row' decremented ('row' == 2), 'i' reset to 1
Iteration: 6 <- prints first * in second row
Iteration: 7 <- prints second * in second row
Iteration: 8 <- 'row' decremented ('row' == 1), 'i' reset to 1
Iteration: 9 <- prints first * in third row
Iteration: 10 <- 'row' decremented ('row' == 0 so no more recursive calls), 'i' reset to 1
Iteration: 9 <- return from recursive call
Iteration: 8 <- return from recursive call
Iteration: 9 <- 'row' decremented, 'i' reset to 1
Iteration: 8
Iteration: 9
Iteration: 8
Iteration: 7
Iteration: 6
Iteration: 7
Iteration: 8
Iteration: 9
Iteration: 8
Iteration: 7
Iteration: 8
Iteration: 7
Iteration: 6
Iteration: 5
Iteration: 6
Iteration: 7
Iteration: 8
Iteration: 7
Iteration: 6
Iteration: 7
Iteration: 6
Iteration: 5
Iteration: 4
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 7
Iteration: 8
Iteration: 9
Iteration: 8
Iteration: 7
Iteration: 8
Iteration: 7
Iteration: 6
Iteration: 5
Iteration: 6
Iteration: 7
Iteration: 8
Iteration: 7
Iteration: 6
Iteration: 7
Iteration: 6
Iteration: 5
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 7
Iteration: 6
Iteration: 5
Iteration: 6
Iteration: 5
Iteration: 4
Iteration: 3
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 7
Iteration: 8
Iteration: 7
Iteration: 6
Iteration: 7
Iteration: 6
Iteration: 5
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 7
Iteration: 6
Iteration: 5
Iteration: 6
Iteration: 5
Iteration: 4
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 5
Iteration: 4
Iteration: 5
Iteration: 4
Iteration: 3
Iteration: 2
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 7
Iteration: 6
Iteration: 5
Iteration: 6
Iteration: 5
Iteration: 4
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 5
Iteration: 4
Iteration: 5
Iteration: 4
Iteration: 3
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 4
Iteration: 3
Iteration: 4
Iteration: 3
Iteration: 2
Iteration: 1
row我通過將方法更改為以下內容,將和的值列印i到上述代碼debug中:
private static void debug(int row, int i) {
for (int j = 0; j < iteration; j ) {
System.out.print(" ");
}
System.out.printf("Iteration %d: row = %d , i = %d%n", iteration, row, i);
}
這是修改后的輸出:
Iteration 1: row = 3 , i = 1
Iteration 2: row = 3 , i = 2
Iteration 3: row = 3 , i = 3
Iteration 4: row = 3 , i = 4
Iteration 5: row = 2 , i = 1
Iteration 6: row = 2 , i = 2
Iteration 7: row = 2 , i = 3
Iteration 8: row = 1 , i = 1
Iteration 9: row = 1 , i = 2
Iteration 10: row = 0 , i = 1
Iteration 9: row = 1 , i = 1
Iteration 8: row = 1 , i = 2
Iteration 9: row = 0 , i = 1
Iteration 8: row = 1 , i = 1
Iteration 7: row = 2 , i = 1
Iteration 6: row = 2 , i = 3
Iteration 7: row = 1 , i = 1
Iteration 8: row = 1 , i = 2
Iteration 9: row = 0 , i = 1
Iteration 8: row = 1 , i = 1
Iteration 7: row = 1 , i = 2
Iteration 8: row = 0 , i = 1
Iteration 7: row = 1 , i = 1
Iteration 6: row = 2 , i = 1
Iteration 5: row = 2 , i = 2
Iteration 6: row = 1 , i = 1
Iteration 7: row = 1 , i = 2
Iteration 8: row = 0 , i = 1
Iteration 7: row = 1 , i = 1
Iteration 6: row = 1 , i = 2
Iteration 7: row = 0 , i = 1
Iteration 6: row = 1 , i = 1
Iteration 5: row = 2 , i = 1
Iteration 4: row = 3 , i = 1
Iteration 3: row = 3 , i = 4
Iteration 4: row = 2 , i = 1
Iteration 5: row = 2 , i = 2
Iteration 6: row = 2 , i = 3
Iteration 7: row = 1 , i = 1
Iteration 8: row = 1 , i = 2
Iteration 9: row = 0 , i = 1
Iteration 8: row = 1 , i = 1
Iteration 7: row = 1 , i = 2
Iteration 8: row = 0 , i = 1
Iteration 7: row = 1 , i = 1
Iteration 6: row = 2 , i = 1
Iteration 5: row = 2 , i = 3
Iteration 6: row = 1 , i = 1
Iteration 7: row = 1 , i = 2
Iteration 8: row = 0 , i = 1
Iteration 7: row = 1 , i = 1
Iteration 6: row = 1 , i = 2
Iteration 7: row = 0 , i = 1
Iteration 6: row = 1 , i = 1
Iteration 5: row = 2 , i = 1
Iteration 4: row = 2 , i = 2
Iteration 5: row = 1 , i = 1
Iteration 6: row = 1 , i = 2
Iteration 7: row = 0 , i = 1
Iteration 6: row = 1 , i = 1
Iteration 5: row = 1 , i = 2
Iteration 6: row = 0 , i = 1
Iteration 5: row = 1 , i = 1
Iteration 4: row = 2 , i = 1
Iteration 3: row = 3 , i = 1
Iteration 2: row = 3 , i = 3
Iteration 3: row = 2 , i = 1
Iteration 4: row = 2 , i = 2
Iteration 5: row = 2 , i = 3
Iteration 6: row = 1 , i = 1
Iteration 7: row = 1 , i = 2
Iteration 8: row = 0 , i = 1
Iteration 7: row = 1 , i = 1
Iteration 6: row = 1 , i = 2
Iteration 7: row = 0 , i = 1
Iteration 6: row = 1 , i = 1
Iteration 5: row = 2 , i = 1
Iteration 4: row = 2 , i = 3
Iteration 5: row = 1 , i = 1
Iteration 6: row = 1 , i = 2
Iteration 7: row = 0 , i = 1
Iteration 6: row = 1 , i = 1
Iteration 5: row = 1 , i = 2
Iteration 6: row = 0 , i = 1
Iteration 5: row = 1 , i = 1
Iteration 4: row = 2 , i = 1
Iteration 3: row = 2 , i = 2
Iteration 4: row = 1 , i = 1
Iteration 5: row = 1 , i = 2
Iteration 6: row = 0 , i = 1
Iteration 5: row = 1 , i = 1
Iteration 4: row = 1 , i = 2
Iteration 5: row = 0 , i = 1
Iteration 4: row = 1 , i = 1
Iteration 3: row = 2 , i = 1
Iteration 2: row = 3 , i = 1
Iteration 1: row = 3 , i = 2
Iteration 2: row = 2 , i = 1
Iteration 3: row = 2 , i = 2
Iteration 4: row = 2 , i = 3
Iteration 5: row = 1 , i = 1
Iteration 6: row = 1 , i = 2
Iteration 7: row = 0 , i = 1
Iteration 6: row = 1 , i = 1
Iteration 5: row = 1 , i = 2
Iteration 6: row = 0 , i = 1
Iteration 5: row = 1 , i = 1
Iteration 4: row = 2 , i = 1
Iteration 3: row = 2 , i = 3
Iteration 4: row = 1 , i = 1
Iteration 5: row = 1 , i = 2
Iteration 6: row = 0 , i = 1
Iteration 5: row = 1 , i = 1
Iteration 4: row = 1 , i = 2
Iteration 5: row = 0 , i = 1
Iteration 4: row = 1 , i = 1
Iteration 3: row = 2 , i = 1
Iteration 2: row = 2 , i = 2
Iteration 3: row = 1 , i = 1
Iteration 4: row = 1 , i = 2
Iteration 5: row = 0 , i = 1
Iteration 4: row = 1 , i = 1
Iteration 3: row = 1 , i = 2
Iteration 4: row = 0 , i = 1
Iteration 3: row = 1 , i = 1
Iteration 2: row = 2 , i = 1
Iteration 1: row = 3 , i = 1
uj5u.com熱心網友回復:
問題的根源在于您設計方法的方式。盡管您有 2 個,但實際上只有一個只是包裝對第二個方法的呼叫,該方法負責兩者:列印單獨的行,并連續列印每個星號。
出于這個原因,當您呼叫該方法以列印單個星號時,實際上它會產生新的遞回呼叫早午餐。
有兩種方法可以避免:
您可以使用一種方法來實作此任務,回圈列印每一行:
public static void printRecursion(int row) {
if (row <= 0) return;
for (int i = 0; i < row; i ) { // printing a row of asterisks
System.out.print("* ");
}
System.out.println(); // advancing output to the next row
printRecursion(row - 1);
}
或者,如果您不允許使用回圈。您可以引入一個負責列印一行星號的輔助方法:
public static void printRecursion(int row) {
if (row <= 0) return;
printRow(row); // printing a row of asterisks
System.out.println(); // advancing output to the next row
printRecursion(row - 1);
}
public static void printRow(int col) {
if (col <= 0) return;
System.out.print("* ");
printRow(col - 1);
}
main()
public static void main(String[] args) {
printRecursion(4);
}
輸出:
* * * *
* * *
* *
*
uj5u.com熱心網友回復:
洗掉對 printRecursion2 的第一個呼叫并將第二個 if 更改為一段時間,例如
private static void printRecursion2(int row,int i) {
if(row<=0)
{
return;
}
while(i<=row)
{
System.out.print("* ");
i ;
}
System.out.println();
i=1;
printRecursion2(row-1,i);
}
但更簡單的方法是擁有這個單一功能:
public static void printRecursion(int row) {
if (row <= 0) return;
System.out.println("* ".repeat(row));
printRecursion(row - 1);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/505866.html
上一篇:涉及求和值的串列理解
下一篇:查找陣列中的重復項
