侧边栏壁纸
  • 累计撰写 53 篇文章
  • 累计收到 5 条评论

Chapter 05

bbchen
2022-09-15 / 0 评论 / 31 阅读 / 正在检测是否收录...

流程控制

化繁为简,先死后活

一道题领会循环:

打印空三角,如图所示:

image-20220827172544765

import java.util.*;

public class Stars {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Input floor:");
        int floor = scanner.nextInt();
        scanner.close();
        for (int i = 1; i <= floor; i++) {

            for(int j = 1; j <= floor - i; j++){
                System.out.print(" ");
            }

            for(int k = 1; k <= 2 * i - 1; k++){
                if(k == 1 || k == 2 * i -1 || i == floor){
                        System.out.print("*");                 
                }
                else{
                    System.out.print(" ");
                }
            }

            System.out.print("\n");
        }
    }
}
0

评论

博主关闭了所有页面的评论