【问题标题】:If else statement not running C++如果 else 语句未运行 C++
【发布时间】:2021-07-20 19:30:32
【问题描述】:

我试图做一个简单的猜数字游戏,当答案大于数字时,if else 语句不会触发。 例如,如果数字是 20,而我选择 30 以上的数字,比如 41,它只会结束代码。

#include <iostream>
#include <string>
#include <cmath>
#include <unistd.h>
#include <chrono>
#include <thread>
#include <fstream>
#include <random>
#include <time.h>
#include "functions.h"

using namespace std;

void game() {
  int number;
  int answer;
  cout << "Welcome to the guess the number game!" << endl;
  cout << "The computer will generate a random number from 1 to 100 and you will try to guess it."<< endl;
  cont();
  ran(number,100,1);
  START:
  cout << number;
  system("clear");
  cout << "Type your answer here: ";
  if (!(cin >> answer)) {
    cin.clear();
    cin.ignore(10000,'\n');
    cout << "THAT IS NOT AN OPTION!" << endl;
    sleepcp(250);
    system("clear");
    goto START;
  }
  int warmer1;
  int warmer2;
  warmer1 = number + 11;
  warmer2 = number - 11;
  if (answer > warmer2) {
    if (answer == number) {
      cout << "You got it!";
    } else if (answer < warmer1) {
      cout << "You are close."<< endl;
      sleepcp(250);
      system("clear");
      goto START;
    }
  } else if (answer > number) {

    cout << "Less." << endl;
    sleepcp(250);
    system("clear");
    goto START;
  } else if (answer < number) {
    cout << "More."<< endl;
    sleepcp(250);
    system("clear");
    goto START;
  }
   
}  

int main() {
  game();
  return 0;
}

谁能帮忙解决一下谢谢!!!

【问题讨论】:

  • goto? do..while 循环会更合适。如果您立即system("clear") 显示,打印出number 有什么意义?
  • 哪个 if/else 不会“触发”?第一个?其他人之一?输入是什么?输出是什么?在调试器中单步执行程序时发生了什么?
  • 第二个 if/else 没有触发。
  • 请不要使用goto
  • @DiazMichael "它是来自函数的随机数生成器。 minimal reproducible example.

标签: c++ if-statement goto


【解决方案1】:

当 number 等于 20 且 answer 等于 41 时考虑这个 if 语句。

  if (answer > warmer2) {
    if (answer == number) {
      cout << "You got it!";
    } else if (answer < warmer1) {
      cout << "You are close."<< endl;
      sleepcp(250);
      system("clear");
      goto START;
    }
  } else if (answer > number) {

在这种情况下,if 语句获得了控制权,因为 answer 大于 warmer2。但是 answer 不等于 number(第一个内部 if 语句)并且 answer 不小于 warmer1。

在这种情况下,什么都没有发生,控制权被传递到程序的末尾。

即 if 这个 if 语句

  if (answer > warmer2) {

获得控制权,然后是下面的 if 语句,例如 this

  } else if (answer > number) {

将被跳过。

换句话说,你所做的就是你得到的。

如果您使用例如 while 或 do-while 循环而不是 goto 语句,则可以解决您的问题。

【讨论】:

    【解决方案2】:

    如果您要使用调试器单步调试您的代码(或至少打印出一些描述游戏决策的诊断消息),您会确切地看到程序终止的原因。

    假设number20answer41,那么warmer131warmer29,因此:

    void game() {
      ...
      START:
      ...
      if (answer > warmer2) { // 41 > 9 is TRUE
        if (answer == number) { // 41 == 20 is FALSE
          ...
        } else if (answer < warmer1) { // 41 < 31 is FALSE
          ...
          goto START; // <-- NOT REACHED!
        }
        // <-- execution reaches here!
      } else if (answer > number) { // <-- NOT EVALUATED!
        ...
        goto START; // <-- NOT REACHED!
      } else if (answer < number) { // <-- NOT EVALUATED!
        ...
        goto START; // <-- NOT REACHED!
      }
      // <-- execution reaches here!   
    }
    

    由于game() 函数没有到达goto START; 语句,循环结束并且game() 退出,因此main() 退出,终止程序。

    在现代 C++ 编码中几乎没有充分的理由使用 goto。请改用whiledo..while 循环,例如:

    #include <iostream>
    #include <limits>
    #include "functions.h"
    
    using namespace std;
    
    void game() {
        int number, answer, warmer1, warmer2;
        cout << "Welcome to the guess the number game!" << endl;
        cout << "The computer will generate a random number from 1 to 100 and you will try to guess it." << endl;
        cont();
        ran(number, 100, 1);
        warmer1 = number + 11;
        warmer2 = number - 11;
        do {
            system("clear");
            cout << "Type your answer here: ";
            if (!(cin >> answer)) {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "THAT IS NOT AN OPTION!" << endl;
            }
            else if (answer == number) {
                cout << "You got it!";
                break;
            }
            else {
                if (answer <= warmer1 && answer >= warmer2) {
                    cout << "You are close." << endl;
                }
                if (answer > number) {
                    cout << "Less." << endl;
                }
                else {
                    cout << "More."<< endl;
                }
            }
            sleepcp(250);
        }
        while (true);
    }  
    
    int main() {
        game();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 2016-04-25
      • 2022-12-03
      • 1970-01-01
      相关资源
      最近更新 更多