我正在尝试将用户输入传递给二维数组。我问用户如下:
我试图计算用户结束后达到的基数。我的代码是:
int p;
int[,] matrix1 = new int[11, 5];
do
{
Console.WriteLine("Enter player number");
p = Convert.ToInt32(Console.ReadLine());
int b;
Console.WriteLine("Enter Bases achieved");
b = Convert.ToInt32(Console.ReadLine());
matrix1[p, b] = b + 1;
Console.WriteLine(matrix1[p, b]);
} while (p < 99);
我意识到这只会为用户输入的基数增加1,并且实际上不会计算总数。我是编程新手。任何人都可以通过解释我如何计算这个12乘5阵列中每个单元格的结果来帮忙吗?
答案 0 :(得分:0)
试试这个:
$my_sum=0; //var for sum
foreach ($martix1 as $row) { //looping thorugh matrix by, fetching rows(first index)
foreach ($row as $cell) {// fetching cells out of each row
echo "$cell" . PHP_EOL; //printing each cell for your pleasure
$my_sum+=$cell; // adding to my_sum which holds the total sum
}
}
echo "the total sum is $my_sum";