java输出钻石,使用递归在Java中以正确格式打印钻石图案

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

nJmpY.png

Conclusions:

The total number of characters on every line is always n (=3)

Number of Spaces has the following pattern:

1st line 3 - 1 spaces

2nd line 3 - 2 spaces

3rd line 3 - 3 spaces

4th line 4 - 3 spaces

5th line 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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值