我做的有什么问题......虽然是逻辑,但继续逻辑?

时间:2017-04-05 21:50:04

标签: c++ random do-while

我是stackoverflow的新手,也是编程的新手,所以请不要介意我的代码格式不佳。我的代码有两个问题。

  1. 我的继续声明,如果玩家输入' y'或者' Y',不起作用。它只在 正确猜测之后终止程序,这导致我:
  2. 2.我继续计数器不停地超过0,我只是看不到程序逻辑中的错误。

    我无法看到我的逻辑问题。

        #include "stdafx.h"
        #include <iostream>
        #include <iomanip>
        #include <ctime>
        #include <random>
    
        using namespace std;
    
        int getNumber(); //random number prototype
        double getScore(); //gets score
        int chances = 7; //chances to guess with
        int main()
        {
        int guess = 0, 
            random;
        char retry = 'y'; //initialize retry to 'y'
        cout << "This is a random number guessing game. " << "You will be guessing between 1-100."
         << "You have 7 chances. Good luck! \n \n" << endl;
    
    
        random = getNumber(); //give the function a variable
    
      do
      {
    
        cout << random << "\n" << "\n";
        chances--;
    
        cout << "Enter your guess: ";
        cin >> guess;
    
            if (guess == random)
            {
                cout << "You have won the game! " << "Your score was: " << getScore();
    
                cout << "Would you like to retry? (Y or N): ";
                cin >> retry;
    
                if (retry == 'y' || retry == 'Y')
                {
                    chances = 7;
                    guess = 0;
                    getNumber();
                    continue; //player can retry the game
                }
                else if (chances == 0)
                {
                    cout << "You have no chances left. Retry? (Y or N): ";
                        cin >> retry;
                    if (retry == 'y' || retry == 'Y')
                    {
                        chances = 7;
                        guess = 0;
                        getNumber();
                        continue;
                    }
                }
    
                    return 0;
            }
            else if (guess != random)
                cout << "You got it wrong. \n" << "You have: " << chances << " chances left" << endl << endl;
            else
                cout << "Incorrect Input. Please type a number." << endl << endl;
       } while (guess != random);
    
    
    return 0;
    }
    
     int getNumber()
        {
         unsigned seed = time(0); //seed the random number
         srand(seed);
    
         int randNum = rand() % 10 + 1; //random number in the range of 1-10
         return randNum;
        }
    

2 个答案:

答案 0 :(得分:2)

if (retry == 'y' || 'Y')

这是不正确的逻辑,这就是为什么你的代码不能按照你想要的方式工作的原因。你希望它是:

if (retry == 'y' || retry == 'Y')

在您的其他if-else语句中修复此逻辑错误。

答案 1 :(得分:1)

你想看看this

您的Int32[] deck = new Int32[52]; for( Int32 i = 0; i < deck.Length; i++ ) { Int32 suit = i / 13 == 0 ? Spade : i / 13 == 1 ? Heart : i / 13 == 2 ? Diamond : Club; deck[i] = CreateCard( suit, i % 13 ); } Int32[] hand = new Int32[4]; for( Int32 i = 0; i < hand.Length; i++ ) { hand[i] = deck[ GetRandomIndexPreviouslyUnused() ]; } 语句跳到最后并检查条件continue,其评估为false并退出guess != random。您需要做的是将guess重置为一个值,例如0,以便条件评估为do while