根据坐标打印星形图案

问题描述 投票:0回答:1

我是java新手,我想根据坐标打印一个反向的星形图案。当我设置好坐标后,它就会打印出星形图案。

This is the expected and how to do it

import java.util.Scanner;
public class coorTest {
    public static void main(String[] args) {
        int max = 5;
        for (int y = 0; y < max; y += 2) {
            int left_spacing = (int) Math.floor(y * 1.0 / 2.0);

            for (int space = 0; space < left_spacing; space++) {
                System.out.print(" ");
            }

            for (int x = 0; x < (max - y); x++) {
                System.out.print("x");
            }
            System.out.println();
        }
    }
}
java
1个回答
0
投票

试试这个。我使用(0,0)作为第一个坐标。

public static void printStar(int x, int y) {
    int starCount = 5;
    int space = 0;
    int count = 0;

    // y-axis line move
    for(int i=0; i<y; i++) {
        System.out.println();
    }

    for (int i = 0; i < starCount; i++) {

        // x-axis space move
        for(int xAxis=0; xAxis<x; xAxis++) {
            System.out.print(" ");
        }

        for (int j = 0; j < starCount; j++) {
            if (count < space) {
                System.out.print(" ");
                count++;
                continue;
            } else if (j >= starCount - count) {
                System.out.print(" ");
            } else {
                System.out.print("*");
            }
        }
        System.out.println();
        space++;
        count = 0;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.