Java模式设计(Java Pattern Design)
我想在java上创建这个模式:
******* ****** ***** **** *** ** *但是,到目前为止我只能生成这段代码:
public class Pattern { public static void main(String [] args) { int height = 7; for (int i = 0; i <= height; i++ ) { for (int j = 0; j <= i ; j++) { System.out.print("*"); } System.out.println(); } } }此代码只能生成此图像:
* ** *** **** ***** ****** ******* ********我应该更改哪些代码才能生成第一张图像?
感谢您的帮助,如果这是一个错误的问题我很抱歉,但我非常感谢您的帮助。
I am trying to make this pattern on java:
******* ****** ***** **** *** ** *However, till now I have only been able to produce this code:
public class Pattern { public static void main(String [] args) { int height = 7; for (int i = 0; i <= height; i++ ) { for (int j = 0; j <= i ; j++) { System.out.print("*"); } System.out.println(); } } }This code can only produce this image:
* ** *** **** ***** ****** ******* ********What part of the code should I change to be able to produce the first image?
Thanks for your help, I apologize if this is a wrong question but I would much appreciate the help.
最满意答案
int height = 7; for (int i = height; i>=1; i-- ) { //reverse,and change '0' to '1' int space_cnt = height - i; //number of space we need for (int j = 1; j <= height ; j++) { if(space_cnt-->0){ System.out.print(" "); }else{ System.out.print("*"); } } System.out.println(); } int height = 7; for (int i = height; i>=1; i-- ) { //reverse,and change '0' to '1' int space_cnt = height - i; //number of space we need for (int j = 1; j <= height ; j++) { if(space_cnt-->0){ System.out.print(" "); }else{ System.out.print("*"); } } System.out.println(); }更多推荐
发布评论