C#-为什么这个程序会给我一个错误?
我正在为我的程序设计考试而练习,这是考试之一。这个程序在第一个for循环后崩溃,我不知道为什么。请帮忙C#-为什么这个程序会给我一个错误?,c#,C#,我正在为我的程序设计考试而练习,这是考试之一。这个程序在第一个for循环后崩溃,我不知道为什么。请帮忙 int n = int.Parse(Console.ReadLine()); Console.WriteLine("{0}{1}{0}", new string('-', n / 2), new string('*', n + 2)); for (int i = 0; i < n - 1; i++) { Console.WriteLin
int n = int.Parse(Console.ReadLine());
Console.WriteLine("{0}{1}{0}", new string('-', n / 2), new string('*', n + 2));
for (int i = 0; i < n - 1; i++)
{
Console.WriteLine("{0}*{1}*{0}", new string('-', n / 2), new string('-', n));
}
for (int i = 0; i < n; i++)
{
Console.WriteLine(
"{0}{1}{2}{1){0}",
new string('-', ((n - 1) / 2) - i),
new string('*', 1 + 2 * i),
new string('-', n - 2 * i));
if (i < n / 2)
{
i++;
}
else
{
i--;
}
}
int n=int.Parse(Console.ReadLine());
WriteLine({0}{1}{0}),新字符串('-',n/2),新字符串('*',n+2));
对于(int i=0;i
您的字符串格式错误,您需要将{1)
更改为{1}
,但输入为5时,代码似乎永远不会结束!其他输入也会出现另一个异常,您能解释一下您想要做什么吗
"{0}{1}{2}{1}{0}",
我现在将您的代码更改为方法
static void PrintDemo(int num)
{
if (num < 0 || num % 2 == 0)
{
return;//do nothing
}
Console.WriteLine("{0}{1}{0}", new string('-', num / 2), new string('*', num + 2));
for (int i = 0; i < num - 1; i++)
{
Console.WriteLine("{0}*{1}*{0}", new string('-', num / 2), new string('-', num));
}
for (int i = 0; i < num; i++)
{
var t1 = Math.Abs(num / 2 - i);
var t2 = t1 * 2 + 1;
var t3 = (num * 2 + 1 - t1 * 2 - t2) / 2;
Console.WriteLine(
"{0}{1}{2}{1}{0}",
new string('-', t1),
new string('*', t3),
new string('-', t2));
}
}
静态void打印演示(int num)
{
如果(num<0 | | num%2==0)
{
return;//什么也不做
}
WriteLine({0}{1}{0}),新字符串('-',num/2),新字符串('*',num+2));
for(int i=0;i
第一个错误已由在此之后,您将获得ArgumentOutOfRangeException。为此,您必须将第二个forloop条件更改为
for (int i = 0; i < (n-1)/2; i++)
{
....
}
当i增加到(n-1)/2时,你的第二个论点变得否定
编辑这是最后一段代码。添加Console.Readline将保持控制台运行,并使您能够查看输出
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
Console.WriteLine("{0}{1}{0}", new string('-', n / 2), new string('*', n + 2));
for (int i = 0; i < n - 1; i++)
{
Console.WriteLine("{0}*{1}*{0}", new string('-', n / 2), new string('-', n));
}
for (int i = 0; i < (n - 1) / 2; i++)
{
Console.WriteLine(
"{0}{1}{2}{1}{0}",
new string('-', ((n - 1) / 2) - i),
new string('*', 1 + 2 * i),
new string('-', n - 2 * i));
if (i < n / 2)
{
i++;
}
else
{
i--;
}
}
Console.ReadLine();
}
static void Main(字符串[]args)
{
int n=int.Parse(Console.ReadLine());
WriteLine({0}{1}{0}),新字符串('-',n/2),新字符串('*',n+2));
对于(int i=0;i
错误是怎么说的?如果没有错误消息,我必须对质量/努力的不足进行否决。当输入是双数字(如4,6)时,您想打印什么?它总是一个奇数。是的,这就像我在这里的第二篇文章。很抱歉,这个问题太糟糕了。我在发布代码时遇到了一些问题。@BradleyDotNET我仍然我不明白他们在说什么…:/@Loocid哦,我看到了区别,看看天空中的括号变成了{1}@Loocid这真的很难发现,但是看看第二个“1”周围的括号@Sky Fang请在你的回答中添加一点解释当我这样做时,代码永远不会结束?如果你修复了格式异常和循环,程序应该结束。这是我看到的输出。你能发布所有代码吗?
new string('-', ((n - 1) / 2) - i)
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
Console.WriteLine("{0}{1}{0}", new string('-', n / 2), new string('*', n + 2));
for (int i = 0; i < n - 1; i++)
{
Console.WriteLine("{0}*{1}*{0}", new string('-', n / 2), new string('-', n));
}
for (int i = 0; i < (n - 1) / 2; i++)
{
Console.WriteLine(
"{0}{1}{2}{1}{0}",
new string('-', ((n - 1) / 2) - i),
new string('*', 1 + 2 * i),
new string('-', n - 2 * i));
if (i < n / 2)
{
i++;
}
else
{
i--;
}
}
Console.ReadLine();
}