题解 | #X形图案#

X形图案

https://www.nowcoder.com/practice/83d6afe3018e44539c51265165806ee4

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int f = a,f1 = 0,f2 = a-1;
            for(int i = 0;i < a;i++){//打印上半部分
                for(int j = 0;j<f;j++){//一行
                    if(j == f1){
                        System.out.print("*");
                    }
                    else if(j==f2){
                        System.out.print("*");
                    }
                    else{
                        System.out.print(' ');
                    }
                    }
                    System.out.printf("\n");
                f1++;f2--;            
            }
        }
    }
}

全部评论

相关推荐

2025-12-17 12:08
门头沟学院 产品经理
牛客85811352...:1希音不知道算不算大厂 2完全符合,过得很舒服, 3确实只有杂活 领导找我续签到明年3、4月我要继续吗。主要是边实习边秋招这段时间还是有点累
什么是优秀的实习经历
点赞 评论 收藏
分享
溱元:前端每年固定死几次,看两集广告就复活了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务