3

私は一生これを理解することはできません。

int Warrior :: attack ()
{
  int hit;
  srand(time(0));

if (Warrior.weapon == 6)
    int hit = rand() % 5 + 1;
else if (Warrior.weapon == 7)
    int hit = rand() % 7 + 4;
else if (Warrior.weapon == 8)
    int hit = rand() % 7 + 9;
else if (Warrior.weapon == 9)
    int hit = rand() % 7 + 14;
else if (Warrior.weapon == 10)
    int hit = rand() % 7 + 19;

std::cout<< "You hit " << hit <<"!\n";

return hit;
}

次のエラーが表示されます: Error C2059: syntax error : '.' (また、のswitch代わりにステートメントを使用する必要があることもわかっていますelse if)

ありがとうございました。

4

1 に答える 1

9

Warriorクラスの名前です。メンバー関数の内部にいる場合は、データメンバーをクラスの名前で修飾する必要はありません。hitまた、if-then-elseのチェーンの前に宣言する必要があります。

int hit;
if (weapon == 6)
    hit = rand() % 5 + 1;
else if (weapon == 7)
    hit = rand() % 7 + 4;
else if (weapon == 8)
    hit = rand() % 7 + 9;
else if (weapon == 9)
    hit = rand() % 7 + 14;
else if (weapon == 10)
    hit = rand() % 7 + 19;

おそらく、ステートメント、またはと値switchのペアの配列を使用したほうがよいでしょう。%+

int mod[] = {0,0,0,0,0,0,5,7,7,7,7};
int add[] = {0,0,0,0,0,0,1,4,9,14,19};
int hit = rand() % mod[weapon] + add[weapon];

上記の配列でweapon、が8の場合、mod[weapon]7、であり、add[weapon]はであり、ステートメント9のデータと一致します。if

于 2012-09-18T21:37:08.580 に答える