C# 读取整数输入并打印矩阵
我试图编写一个程序,从控制台读取一个正整数N(N<20)并打印一个如下矩阵:C# 读取整数输入并打印矩阵,c#,matrix,C#,Matrix,我试图编写一个程序,从控制台读取一个正整数N(N=20 | | N只需将此行Console.WriteLine(row);更改为此Console.WriteLine(); 这里的问题是:在每个内部循环的末尾,您将再次写入行值;这是不需要的。只需将此行更改为Console.WriteLine(row);即可Console.WriteLine(); 这里的问题是:在每个内部循环的末尾,您再次写入行值;这是不需要的。第一个问题是您认为Console.WriteLine(行)是什么是在做什么?当你学习编
N = 3
1 2 3
2 3 4
3 4 5
N = 5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
这是我的代码:
using System;
namespace _6._12.Matrix
{
class Program
{
static void Main()
{
Console.WriteLine("Please enter N ( N < 20): ");
int N = int.Parse(Console.ReadLine());
int row;
int col;
for (row = 1; row <= N; row++)
{
for (col = row; col <= row + N - 1; )
{
Console.Write(col + " ");
col++;
}
Console.WriteLine(row);
}
Console.WriteLine();
}
}
}
使用系统;
命名空间_6._12.1矩阵
{
班级计划
{
静态void Main()
{
Console.WriteLine(“请输入N(N<20):”;
int N=int.Parse(Console.ReadLine());
int行;
int col;
对于(row=1;rowsimple,更改Console.WriteLine(row);
forConsole.WriteLine();
当你在做的时候
static void Main()
{
int N;
do
{
Console.Write("Please enter N (N >= 20 || N <= 0): ");
}
while (!int.TryParse(Console.ReadLine(), out N) || N >= 20 || N <= 0);
for (int row = 1; row <= N; row++)
{
for (int col = row; col <= row + N - 1; )
{
Console.Write(col + " ");
col++;
}
Console.WriteLine();
}
Console.Read();
}
Please enter N (N >= 20 || N <= 0): 5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
static void Main()
{
int N;
做
{
Console.Write(“请输入N(N>=20 | | N简单,更改Console.WriteLine(行);
用于Console.WriteLine();
当你在做的时候
static void Main()
{
int N;
do
{
Console.Write("Please enter N (N >= 20 || N <= 0): ");
}
while (!int.TryParse(Console.ReadLine(), out N) || N >= 20 || N <= 0);
for (int row = 1; row <= N; row++)
{
for (int col = row; col <= row + N - 1; )
{
Console.Write(col + " ");
col++;
}
Console.WriteLine();
}
Console.Read();
}
Please enter N (N >= 20 || N <= 0): 5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
static void Main()
{
int N;
做
{
Console.Write(“请输入N(N>=20 | | N只需将此行Console.WriteLine(row);
更改为此Console.WriteLine();
这里的问题是:在每个内部循环的末尾,您将再次写入行值;这是不需要的。只需将此行更改为Console.WriteLine(row);
即可Console.WriteLine();
这里的问题是:在每个内部循环的末尾,您再次写入行值;这是不需要的。第一个问题是您认为Console.WriteLine(行)是什么是在做什么?当你学习编程时,关键是要“看到”代码在做什么以及为什么要这样做,而不是运行它,调整它,然后再次运行它,看看它是否按你希望的方式运行。一旦你在头脑中清楚而简洁地看到代码在做什么,你就会注意到Console.WriteLine(行)这是不对的,你只需要在那一点上写一行新行。第一个问题是你认为Console.WriteLine(行)是什么是在做什么?当你学习编程时,关键是要“看到”代码在做什么以及为什么要这样做,而不是运行它,调整它,然后再次运行它,看看它是否按你希望的方式运行。一旦你在头脑中清楚而简洁地看到代码在做什么,你就会注意到Console.WriteLine(行)这是不对的,您只需要在这一点上写一个换行。这里是另一种使用if
语句的方法,而不是使用do while
,代码看起来更简单一些:
static void Main(string[] args)
{
Console.Write("Give a number from 1 to 20: ");
int n = int.Parse(Console.ReadLine());
int row,col;
Console.WriteLine("");
if (n > 0 && n < 21)
{
for (row = 1; row <= n; row++)
{
for (col = row; col <= row + n - 1;col++ )
{
Console.Write(col + " ");
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("This number is greater than 20 or smaller than 1");
}
}
static void Main(字符串[]args)
{
控制台。写(“给出一个从1到20的数字:”;
int n=int.Parse(Console.ReadLine());
int row,col;
控制台。写线(“”);
如果(n>0&&n<21)
{
对于(row=1;row,这里是另一种使用if
语句而不是使用do while
的方法,代码看起来更简单一些:
static void Main(string[] args)
{
Console.Write("Give a number from 1 to 20: ");
int n = int.Parse(Console.ReadLine());
int row,col;
Console.WriteLine("");
if (n > 0 && n < 21)
{
for (row = 1; row <= n; row++)
{
for (col = row; col <= row + n - 1;col++ )
{
Console.Write(col + " ");
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("This number is greater than 20 or smaller than 1");
}
}
static void Main(字符串[]args)
{
控制台。写(“给出一个从1到20的数字:”;
int n=int.Parse(Console.ReadLine());
int row,col;
控制台。写线(“”);
如果(n>0&&n<21)
{
for(row=1;row//以上所有答案我都错了,你应该试一下,然后回答我
Console.Write("Enter N: (N < 20) ");
int n = Int32.Parse(Console.ReadLine());
for (int row = 1; row <= n;row++)
{
Console.Write(row+" ");
for (int col = row+1; col <= row + n - 1; )
{
Console.Write(col + " ");
col++;
}
Console.WriteLine();
}
Console.ReadLine();
Console.Write(“输入N:(N<20)”;
intn=Int32.Parse(Console.ReadLine());
for(int row=1;row//以上所有答案我都错了,你应该尝试一下,然后回答我
Console.Write("Enter N: (N < 20) ");
int n = Int32.Parse(Console.ReadLine());
for (int row = 1; row <= n;row++)
{
Console.Write(row+" ");
for (int col = row+1; col <= row + n - 1; )
{
Console.Write(col + " ");
col++;
}
Console.WriteLine();
}
Console.ReadLine();
Console.Write(“输入N:(N<20)”;
intn=Int32.Parse(Console.ReadLine());
对于(int row=1;row使用系统;
命名空间_6._12.1矩阵
{
班级计划
{
静态void Main()
{
Console.WriteLine(“请输入N(N<20):”;
int N=int.Parse(Console.ReadLine());
int行;
int col;
对于(行=1;行使用系统;
命名空间_6._12.1矩阵
{
班级计划
{
静态void Main()
{
Console.WriteLine(“请输入N(N<20):”;
int N=int.Parse(Console.ReadLine());
int行;
int col;
对于(row=1;row First check NON First check NOkey。额外的列被取消,但现在一个数字:N+1出现在第一列下面,这是不需要的。@Angelenkov,你能告诉我在哪里吗?好的。额外的列被取消,但现在一个数字:N+1出现在第一列下面,这是不需要的。@Angelenkov,你能告诉我在哪里吗?