歡迎,我在開關中的小功能問題。 我的問題是 這裏的「未分配的局部變量‘矩陣’的使用」是代碼:開關中的變量,開關循環,情況2中的錯誤
static void Main(string[] args)
{
char wyj = 'n';
do
{
Console.WriteLine("1. add numbers into matrix \n2. show matrix \n3. end");
int a;
Console.Write("\nYour choice: ");
a = int.Parse(Console.ReadLine());
switch (a)
{
case 1:
Console.WriteLine("You choose: 1");
int element;
Console.Write("\nsize of matrix: ");
int matrixsize;
matrixsize = Int32.Parse(Console.ReadLine());
int[,] matrix = new int[matrixsize, matrixsize];
for (int i = 0; i <= matrixsize - 1; i++)
{
for (int j = 0; j <= matrixsize - 1; j++)
{
Console.Write("element{0},{1} =", i + 1, j + 1);
element = int.Parse(Console.ReadLine());
matrix[i, j] = element;
}
}
break;
case 2:
Console.WriteLine("You choose 2");
foreach (int x in matrix)
Console.Write(x);
break;
case 3:
Console.WriteLine("End the program? y- yes, n- no");
wyj = char.Parse(Console.ReadLine());
break;
}
}
while (wyj != 'y');
Console.WriteLine("Koniec programu!");
Console.ReadKey();
}
我需要做什麼?
在Doc Brown答案後,如果沒有任何事情發生,矩陣是空的。 我認爲循環是問題?
代碼審查可能是一個更好的目標,但@DocBrown是正確的。 – itsbruce
@itsbruce不,代碼審查不適用於無法使用的代碼。如果你不知道他們的規則,請不要將其他人發送到其他網站。 – svick