怎么用C#输出一个菱形?
展开全部
public static void Main (string[] args) {
Console.WriteLine(请输入一个奇数);
int x=int.Parse (Console .ReadLine ());
if(x%2==0){
Console .WriteLine("您输入的是偶数,无法生成漂亮的菱形哦!");
}
string s = "";
for (int i = 0; i < x/2; i++) {
for (int j = 0; j<x/2+i+1; j++) {
if ((j == x/2 - i ) ||(j == x/2 + i ))
s += "*";
else
s += " ";
}
s += "\n";
}
for (int i = x/2; i < x; i++) {
for (int j =0;j<x; j++) {
if ((j == i - x/2 ) || (j == 3*x/2-i-1))
s += "*";
else
s += " ";
}
s += "\n";
}
Console.WriteLine (s);
}
Console.WriteLine(请输入一个奇数);
int x=int.Parse (Console .ReadLine ());
if(x%2==0){
Console .WriteLine("您输入的是偶数,无法生成漂亮的菱形哦!");
}
string s = "";
for (int i = 0; i < x/2; i++) {
for (int j = 0; j<x/2+i+1; j++) {
if ((j == x/2 - i ) ||(j == x/2 + i ))
s += "*";
else
s += " ";
}
s += "\n";
}
for (int i = x/2; i < x; i++) {
for (int j =0;j<x; j++) {
if ((j == i - x/2 ) || (j == 3*x/2-i-1))
s += "*";
else
s += " ";
}
s += "\n";
}
Console.WriteLine (s);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询