我的do ... while逻辑和continue逻辑有什么问题? [英] What is wrong with my do...while logic, and continue logic?

查看:70
本文介绍了我的do ... while逻辑和continue逻辑有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to stackoverflow, and also somewhat new to programming, so please don't mind my poor formatting of the code. I have two problems with my code.


  1. 我的continue语句,如果播放器键入 y,我将用来继续循环或 Y,则无效。 only 仅在正确猜测后终止程序,这使我进入:

  1. My continue statement, which I'm using to continue the loop if the player types 'y' or 'Y', doesn't work. It terminates the program after only getting the guess correctly, which leads me to:

2。我继续计数器经过0而没有停止,我只是看不到程序逻辑中的错误。

2.My continue counter goes past 0 without stopping, and I just can't see my error in the logic of the program.

我看不到逻辑问题。

    #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;
    }


推荐答案

您想要看看

您的 continue 语句跳到结尾并检查条件 guess!= random ,该条件的值为false并退出 do while 。您需要做的就是将猜测值重置为0,以便条件的评估结果为 true

Your continue statement is jumping to the end and checking the condition, guess != random, which evaluates to false and exits the do while. What you need to do is reset guess to a value such as 0 so that the condition does evaluate to true.

这篇关于我的do ... while逻辑和continue逻辑有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆