0

ユーザー入力を 2 次元配列に渡そうとしています。ユーザーに次のことを尋ねます。

  • プレイヤー番号 (制限: 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 に答える