我试图写一个程序,从控制台读取一个正整数N(N < 20),并打印像这些的矩阵:读整数输入并打印矩阵
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();
}
}
}
问题是控制台打印一个额外的列,数字从1到N,我不知道如何摆脱它。我有一个想法,为什么这可能会发生,但仍然无法找到解决方案。
首先检查ñ<20或者是否会再次要求用户把一个数<20 – 2012-08-06 06:14:03