0

これは私が作成しているクラスの関数メンバーであり、構文エラーは発生せず、セマンティック エラーのみです。作成するはずの乱数は 0 になります。

int Warrior::attack()
{

int hit = 0;

 switch (weapon)
{

case 6:
     hit = rand() % (5 - 1 + 1) + 1; 
     break;

case 7:
     hit = rand() % (10 - 4 + 1) + 4;
     break;

case 8:
     hit = rand() % (15 - 9 + 1) + 9;
     break;

case 9:
     hit = rand() % (20 - 14 + 1) + 14;
     break;

case 10:
      hit = rand() % (25 - 19 + 1) + 19;
      break;
 }

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

 return hit;
}

助けてください!ありがとう〜カタナ

4

1 に答える 1

2

おそらく、defaultswitch ステートメントがない、つまり句weaponをトリガーしていないことが原因です。switch

試す:

 switch (weapon)
{

case 6:
     hit = rand() % (5 - 1 + 1) + 1; 
     break;

case 7:
     hit = rand() % (10 - 4 + 1) + 4;
     break;

case 8:
     hit = rand() % (15 - 9 + 1) + 9;
     break;

case 9:
     hit = rand() % (20 - 14 + 1) + 14;
     break;

case 10:
      hit = rand() % (25 - 19 + 1) + 19;
      break;
default:
      throw std::exception("switch statement failed");
 }
于 2012-09-23T19:21:45.413 に答える