美文网首页
算法训练 图形显示

算法训练 图形显示

作者: 就这样吧嘞 | 来源:发表于2018-12-28 17:04 被阅读0次

问题描述
  编写一个程序,首先输入一个整数,例如5,然后在屏幕上显示如下的图形(5表示行数):
  * * * * *
  * * * *
  * * *
  * *
  *
结果

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int x=0;x<n;x++) {
            for(int y=0;y<n-x;y++) {
                System.out.print("*");
            }
            System.out.println("");
        }
            
    }
    
}

每个*有空格,修改一下

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int x=0;x<n;x++) {
            for(int y=0;y<n-x;y++) {
                System.out.print("* ");
            }
            System.out.println("");
        }
            
    }
    
}

完成

相关文章

网友评论

      本文标题:算法训练 图形显示

      本文链接:https://www.haomeiwen.com/subject/uvrelqtx.html