輸入一個正整數 n ,輸出九九乘法表的前n*n項,
輸出示樣式如下:
Input n:3
* 1 2 3
1 1
2 2 4
3 3 6 9
代碼如下:
1 public static void sum() 2 { 3 string n = string.Empty; 4 Console.Write("Input n:"); 5 n = Console.ReadLine(); 6 7 string[,] sum = new string[Convert.ToInt32(n) + 1, Convert.ToInt32(n) + 1]; 8 for (int i = 0; i <= Convert.ToInt32(n); i++) 9 { 10 for (int j = 0; j <= Convert.ToInt32(n); j++) 11 { 12 if (i == 0 && j == 0) 13 { 14 } 15 else if (i == 0 || j == 0) 16 { 17 sum[i, j] = Convert.ToString(i + j); 18 } 19 else if (i >= j) 20 { 21 sum[i, j] = Convert.ToString(i * j); 22 } 23 else 24 { 25 sum[i, j] = ""; 26 } 27 } 28 } 29 for (int i = 0; i <= Convert.ToInt32(n); i++) 30 { 31 for (int j = 0; j <= Convert.ToInt32(n); j++) 32 { 33 if (i == 0 && j == 0) 34 { 35 Console.Write(‘*‘ + " "); 36 } 37 else 38 { 39 Console.Write(sum[i, j] + " "); 40 } 41 } 42 Console.WriteLine(); 43 Console.WriteLine(); 44 } 45 Console.ReadLine(); 46 }
时间: 2024-10-08 15:00:07