猿问

如何在java中打印星形图案

我试图找到以下星形图案的任何序列或公式,但我没有找到,所以只是在下面尝试


public class MyClass {

    public static void main(String args[]) {



        System.out.println("....*....");

        System.out.println("...***...");

        System.out.println("*********");

        System.out.println(".*******.");

        System.out.println("*********");

        System.out.println("...***...");

        System.out.println("....*....");


    }

}

这里的 “.” 表示 “” 空格字符。我们可以用循环打印这颗星星吗?

慕侠2389804
浏览 108回答 4
4回答

FFIVE

我使用的是 Java 11。您应该能够绘制星号的任何对映异形图案。有关详细说明,请阅读注释。import java.util.Arrays;class Star {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int[] stars = new int[] { 1, 3, 9, 7, 9, 3, 1 }; // asterisks per line&nbsp; &nbsp; &nbsp; &nbsp; int max = Arrays.stream(stars).max().getAsInt(); // max asterisks in any line&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < stars.length; i++) { // prints the asterisks for a given row, and pads it left and right with spaces&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" ".repeat((max - stars[i]) / 2) + "*".repeat(stars[i]) + " ".repeat((max - stars[i] + 1) / 2));&nbsp; &nbsp; }}

月关宝盒

希望它有帮助!对于低年级学生来说,这很容易理解,而不是创建数组class starPattern {&nbsp; &nbsp; public static void main(String []args) {&nbsp; &nbsp; &nbsp; &nbsp; for(int i=1;i<=7;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=1;j<=9;j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i==3 || i==5 || j==5 || (i==4 && j>1 && j<9) || ((i==2 || i==6) && (j>3 && j<7))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我的电脑老师检查一下,这是正确的方法!关于此模式的简要说明1↓&nbsp; 1→&nbsp; 2&nbsp; &nbsp;3&nbsp; &nbsp;4&nbsp; &nbsp;5&nbsp; &nbsp;6&nbsp; &nbsp;7&nbsp; &nbsp;8&nbsp; &nbsp;92&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*5&nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;6&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

牧羊人nacy

不知道你是什么意思,但如果你真的想要一个循环,这里有一个解决方案!public class Star {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int[] dots = new int[] {4, 3, 0, 1, 0, 3, 4};&nbsp; &nbsp; &nbsp; &nbsp; int width= 9;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < dots.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < width; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (j < dots[i] || j > width - dots[i] - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(".");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

小怪兽爱吃肉

是的,它可以使用循环来完成,不应该通过sout语句来完成。看起来你也是编程的初学者,所以我建议你自己尝试这类问题的逻辑。它将有助于你的成长。只需逐行尝试即可。这些问题通常需要2个循环,一个在垂直方向移动,另一个在水平方向移动。你可以把它想象成一个i*j矩阵(例如3X3,4X4)。因此,自己尝试一下,从一些基本的开始。如果您仍然需要解决方案,请先向我发送 ping,但请先自行尝试。
随时随地看视频慕课网APP

相关分类

Java
我要回答