Java模式设计(Java Pattern Design)

编程入门 行业动态 更新时间:2024-10-27 20:27:09
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(); }

更多推荐

本文发布于:2023-07-23 00:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   Java   Design   Pattern

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!