程序问题注释开始
-
程序的版权和版本声明部分
-
Copyright © 2020,湖南工程职业技术学院信息工程学院学生
-
文件名称: 蓝桥杯赛题
-
作 者: 李 斌
-
完成日期: 2020 年 10 月 14日
-
对任务及求解方法的描述部分
标题:打印大X
如下的程序目的是在控制台打印输出大X。
可以控制两个参数:图形的高度,以及笔宽。
用程序中的测试数据输出效果:
请仔细分析程序流程,填写缺失的代码。
public class A
{
static void f(int h, int w){
System.out.println(String.format("高度=%d, 笔宽=%d",h,w));
int a1 = 0;
int a2 = h - 1;
for(int k=0; k<h; k++){
int p = Math.min(a1,a2);
int q = Math.max(a1+w,a2+w);
for(int i=0; i<p; i++) System.out.print(" ");
if(q-p<w*2){
//填空答案:for(int i=0; i<q-p; i++) System.out.print("*")
____________________________________________ ;
}
else{
for(int i=0; i<w; i++) System.out.print("*");
for(int i=0; i<q-p-w*2; i++) System.out.print(" ");
for(int i=0; i<w; i++) System.out.print("*");
}
System.out.println();
a1++;
a2--;
}
}
public static void main(String[] args){
f(15,3);
f(8,5);
}
}
注意:只填写缺失的代码,不要拷贝已经存在的代码。
第二种做法:编程
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//接收高度和宽度
int n=sc.nextInt();
int m=sc.nextInt();
sc.close();
//初始化二维数组
char[][] ch=new char[n][n+m-1];
for(int i=0;i<n;i++)
{
//左边对角线
for(int j=i;j<i+m;j++)
{
//空格不需要处理,处理*号
ch[i][j]='*';
}
//右边对角线
for(int j=n+m-2-i;j>n-2-i;j--)
{
//空格不需要处理,处理*号
ch[i][j]='*';
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n+m-1;j++)
{
//输出二维数组数字元素
System.out.print(ch[i][j]);
}
//输出换行
System.out.println();
}
}
}