本文介绍了使用递归在Java中以正确格式打印钻石图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序从文件中读取值,并使用递归方法根据这些值打印星号图案。我只是有一个问题,一切正常排队。



输出应该是这样的:

  * 
* *
* * *
* *
*
pre>

关于输出的格式,指示是:

请注意,在中心线对称(垂直)对齐,图案应该在每一行(水平)对称对齐 - 提示:使用行值来帮助空间。



但我的输出如下所示:

  * 
* *
* * *
* *
*

以获得此模式:

  public static void makePattern(int thisRow,int num){
if (thisRow> = num){
for(int i = 0; i< num; i ++){
System.out.print(+*+);
}
System.out.println();

$ b $ {
(int i = 0; i< thisRow; i ++){
System.out.print(+* +);
}
System.out.println();

makePattern(thisRow + 1,num);

(int i = 0; i< thisRow; i ++){
System.out.print(+*+);
}
System.out.println();





$ b

我的主要方法是:

  import java.util.Scanner; 
import java.io. *;
$ b $ public class Program3 {
public static void main(String [] args)throws Exception {
int num = 0;
int thisRow = 1;
java.io.File file = new java.io.File(../ instr / prog3.dat);
扫描仪fin =新扫描仪(文件); $(b
$ b while(fin.hasNext()){
num = fin.nextInt();
if(num> = 0& num< = 25)
makePattern(thisRow,num);
System.out.println();
}
fin.close();




$ p
$ b

有关如何编辑我的代码以使我的输出看起来像我包含的示例模式?

解决方案

我们首先分析输出结果!!



第一步是分析输出


结论:




  • 总数每行的字符总是 n (= 3)
  • 空格的数量有以下模式:

    3 - 1 空格

    3 - 2 空格

    3 - 3个空格

    / strong> - 3个空格

    5 - 3个空格

    / p>

      if(num  numberOfSpace s = thisRow  -  num; 
    } else {
    numberOfSpaces = num - thisRow;

    $ / code $ / pre $
  • 星号总是[ n - 数字


    $ b $ p $ int numberOfStars = num - numberOfSpaces;


  • 递归应该在第6行结束,即当前行号是 n * 2



    所以递归方法中的返回条件应该是

    pre > if(thisRow == num * 2)
    return;








最终代码:将peices放在一起



当我们把peices放在一起时,我们得到:

$ p $终止条件
if(thisRow == num * 2)
return;

//空格的数量
int numberOfSpaces = 0;
if(num numberOfSpaces = thisRow - num;
} else {
numberOfSpaces = num - thisRow;
}

//星星数
int numberOfStars = num - numberOfSpaces;

//在打印字符串之前编写字符串
StringBuffer outputBuffer = new StringBuffer(num);
for(int i = 0; i< numberOfSpaces; i ++){
outputBuffer.append();

for(int i = 0; i< numberOfStars; i ++){
outputBuffer.append(*);
}

//输出字符串
System.out.println(outputBuffer.toString());

//递归
makePattern(thisRow + 1,num);
}


My program reads in values from a file and uses a recursive method to print patterns of asterisks based on those values. I'm just having a problem getting everything to line up properly.

The output is supposed to look like this:

    *
  *   *
*   *   *
  *   *
    *

Regarding the format of the output, the directions are:

"Note that the pattern is aligned symmetrically (vertically) about the center line. The pattern should be aligned symmetrically on each line (horizontally) as well- hint: use the line value to help space."

But my output looks like this:

*
*  *
*  *  *
*  *
*

The code I'm using to get this pattern:

public static void makePattern(int thisRow, int num) {
    if(thisRow >= num) {
        for(int i = 0; i < num; i++) {
            System.out.print("  " + "*" + "  ");
        }
        System.out.println();

    }
    else {
        for(int i = 0; i < thisRow; i++) {
            System.out.print("  " + "*" + "  ");
        }
        System.out.println();

        makePattern(thisRow + 1, num);

        for(int i = 0; i < thisRow; i++) {
            System.out.print("  " + "*" + "  ");
        }
        System.out.println();
    }
}

Also my main method:

import java.util.Scanner;
import java.io.*;

public class Program3 {
    public static void main(String[] args) throws Exception {
        int num = 0;
        int thisRow = 1;
        java.io.File file = new java.io.File("../instr/prog3.dat");
        Scanner fin = new Scanner(file);

        while(fin.hasNext()) {
                num = fin.nextInt();
                if(num >=0 && num <= 25)
                    makePattern(thisRow, num);
                System.out.println();
        }
        fin.close();

    }

Any suggestions on how to edit my code to make my output appear like the example pattern I included?

解决方案

Let's analyse the output first!!

First step is to analyse the output

Conclusions:

  • The total number of characters on every line is always n (=3)
  • Number of Spaces has the following pattern:

    3 - 1 spaces
    3 - 2 spaces
    3 - 3 spaces
    4 - 3 spaces
    5 - 3 spaces

    So

    if(num < thisRow) {
      numberOfSpaces = thisRow - num;
    } else {
      numberOfSpaces = num - thisRow;
    }
    

  • Number of Stars is always [n - the number of spaces]

    So

    int numberOfStars = num - numberOfSpaces;
    

  • And the recursion should end on the 6th line, i.e. when current line number is n*2

    So the return condition in your recursive method should be

    if(thisRow == num * 2)
       return;
    



Final Code : Putting the peices together

When we put the peices together, we get:

    public static void makePattern(int thisRow, int num) {
        //the termination condition
        if(thisRow == num * 2)
           return;

        //the number of spaces
        int numberOfSpaces = 0;
        if(num < thisRow) {
          numberOfSpaces = thisRow - num;
        } else {
          numberOfSpaces = num - thisRow;
        }

        //the number of stars
        int numberOfStars = num - numberOfSpaces;

        //compose the string before printing it
        StringBuffer outputBuffer = new StringBuffer(num);
        for (int i = 0; i < numberOfSpaces; i++){
            outputBuffer.append(" ");
        }
        for (int i = 0; i < numberOfStars; i++){
            outputBuffer.append("* ");
        }

        //print the string
        System.out.println(outputBuffer.toString());

        //recursion
        makePattern(thisRow + 1, num);
   }

这篇关于使用递归在Java中以正确格式打印钻石图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:20