2

だから私はしようとしています:

  1. do while ループ内で二重入力検証を行う
  2. 移動がまだ行われているかどうか、およびそれが論理入力であるかどうかを確認してください。(#1-9)

私はもともと if else ステートメントを考えていましたが、else ステートメントを使用してループの先頭に戻る方法がわかりません。

do
{
  cout << "Interesting move, What is your next choice?: ";
  cin >> play;
  Pused[1] = play;
  if(play != Pused[0] && play != cantuse[0] && play != cantuse[1] )
  {
    switch(play)
    {
      default:cout << "Your choice is incorrect\n\n";
      break;
    }   
  }
  else
  { }
}    
while(play != 1 && play != 2 && play != 3 && play != 4 && play != 5 && play != 6 && play != 7 && play != 8 && play != 9);
Dis_board(board);
4

3 に答える 3

0

ループの最初に戻るには、「continue」キーワードを使用します。

于 2012-12-19T05:48:33.150 に答える
0

あなたの質問は少しわかりにくいですが、このループで解決すべきいくつかの条件があります。

  1. ユーザーに入力を求める
  2. ユーザーの入力が有効かどうかを確認します (1 ~ 9 の間で、以前は使用されていません)。
  3. 有効な選択肢がある場合は、ループを終了します

したがって、どのような動きが行われたかを記録し、ユーザーの入力が有効な選択肢の範囲内であることを確認する必要があります。有効な選択肢が選択された場合にのみ終了するループを使用できます。

int choice;
bool used[9] = { false }; // Set all values to false
std::cout << "Interesting move, what is your next choice?: ";
do {
    std::cin >> choice;
    // Here we check if the choice is within the range we care about
    // and not used, note if the first condition isn't true then
    // the second condition won't be evaluated, so if choice is 11
    // we won't check used[10] because the && will short circuit, this
    // lets us avoid array out of bounds. We also need to
    // offset the choice by 1 for the array because arrays in C++
    // are indexed from 0 so used runs from used[0] to used[8]
    if((choice >= 1 && choice <= 9) && !used[choice - 1]) {
        // If we're here we have a valid choice, mark it as used
        // and leave the loop
        used[choice - 1] = true;
        break; // Exit the loop regardless of the while condition
    }

    // If we've made it here it means the above if failed and we have
    // an invalid choice. Restart the loop!
    std::cout << "\nInvalid choice! Input: ";
} while (true);
于 2012-12-19T23:16:11.033 に答える
0

削除するだけelseです。必須ではないと思います。の条件whileが満たされると、自動的にループが続行されます。

于 2012-12-19T06:05:52.937 に答える