0

我正在尝试将用户输入传递给二维数组。我问用户以下问题:

  • 球员号码(限制:0 - 11)
  • 达到的碱基数(限制:0 - 4)

我正在尝试计算用户结束后达到的碱基数。我的代码是:

    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 x 5 数组中每个单元格的结果?

4

1 回答 1

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";
于 2013-06-08T18:35:16.510 回答