【问题标题】:How do I execute previously executed lines of code in C++如何在 C++ 中执行以前执行的代码行
【发布时间】:2020-04-06 23:07:52
【问题描述】:

我开始在业余时间学习如何使用 C++ 编写代码,使用在线学习 C++ 的人提供给我的不同网站和应用程序。到目前为止,我知道最基本的命令。我尝试了一个程序给出的练习,我得到了某人正在度假的信息,并且需要知道他可以随身携带多少行李。他可以携带多少行李的限制是 45,如果行李低于、高于或等于限制(45 件行李),我必须显示不同的输出。我做了一些编码,最后得到了这个:

#include <iostream>

using namespace std;

int main()
{
    const int limit = 45;
    int bag;
    cout << "Please type your number here: ";
    cin >> bag;
    string yn;
    int keep = 0;
    if (limit < bag)
    {
        cout << "You passed the limit." << endl;
    };
    if (limit == bag)
    {
        cout << "Just enough." << endl;
    };
    if (limit > bag)
    {
        cout << "You got space." << endl;
    };
    ++keep;
    while(keep > 0)
    {
        int keep = 0;
        cout << "Do you want to try another number?" << endl;
        cin >> yn;
        cout << endl;
        if(yn == "yes")
        {
            int bag = 0;
            cout << "Please type your number here: ";
            cin >> bag;
            if (limit < bag)
            {
                cout << "You passed the limit." << endl;
            };
            if (limit == bag)
            {
                cout << "Just enough." << endl;
            };
            if (limit > bag)
            {
                cout << "You got space." << endl;
            };
        }
        else
        {
            return 0;
        }
    }
}

出于我自己对这个问题的兴趣,我已经开发了超出需要的内容-如您所见-。我已经复制并粘贴了上面看到的 3 个 IF 命令,我相信有一种更简单的方法,用更少的代码来解决这个问题。我想到的是,如果我可以返回并再次执行某行代码,无论是从一行及以下(例如从第 45 行及以下),还是从特定的代码行(例如从第 45 行到第 60 行)。 如果您想到另一种解决此问题的方法并在下面发布您的代码,我将不胜感激。 感谢您的回复。

【问题讨论】:

  • 返回“向上”代码的唯一方法是使用循环。我为你想学习 C++ 而鼓掌,但我真的建议你从一本入门书籍 here 开始学习 C++。 C++ 是一种非常复杂和微妙的语言,许多在线资源往往掩盖了一些重要的材料。
  • 这里不要使用行号。没有人知道第 45 行或第 60 行是什么。如有必要,请在代码中添加注释以指示您正在谈论的行。并且不要将; 放在像这样if () {}; 的if 块之后。此外,不要使用if (a == b) {}; if (a &lt; b) {}; if (a &gt; b) {}。使用if (a == b) {} else if (a &lt; b) {} else {}
  • 而不是“如果这个,回到这里”或“如果那个,继续到那里”,想想“在这个条件为真时重复这些行”(你在正确的轨道上) .您可能还想了解breakdo-while 形式的循环。
  • @Pandofla here's a small example 如何使用循环让程序无休止地运行
  • 你已经在代码中有一个循环。小心你的变量。你有两个不同的keepkeep 似乎是为了控制循环,但现在keep 没有做任何事情

标签: c++ c++11 if-statement conditional-statements c++17


【解决方案1】:

我们都在某个时候开始编写我们的第一个 C++ 程序,所以请允许我给你一些额外的反馈:

  • 首先avoid writing using namespace std;
  • 其次,命名——什么是baglimitkeepyn?如果它们被称为bagSizemaximumPermittedBagSizeinputFromUser(你并不真的需要变量keep,见下文),是不是更容易阅读和理解?
  • 最后,这是您的程序的(大致)重构版本,删除了重复项并添加了 cmets。
#include <iostream>

int main()
{
    const int maximumPermittedBagSize = 45;
    // Loops forever, the user exits by typing anything except 'yes' laster
    while(true)
    {
        std::cout << "Please type your number here: " << std::endl;
        //Declare (and initialize!) variables just before you need them
        int bagSize = 0;
        std::cin >> bagSize;
        if (bagSize > maximumPermittedBagSize)
        {
            std::cout << "You passed the limit." << std::endl;
        }
        else if (bagSize == maximumPermittedBagSize )
        {
            std::cout << "Just enough." << std::endl;
        }
        else
        {
            std::cout << "You got space." << std::endl;
        }

        std::cout << "Do you want to try another number?" << std::endl;
        std::string inputFromUser = "";
        std::cin >> inputFromUser;
        std::cout << std::endl;
        //Leave the loop if the user does not answer yes
        if(inputFromUser != "yes")
        {
            return 0;
        }
    }
}

【讨论】:

  • 非常感谢 CharonX 的详细解释。谢谢你告诉我我不应该使用“使用命名空间标准”。关于变量名,由于是小程序,所以我放了小变量名,但我想从现在开始应用一些易于阅读和理解的变量名是一个好习惯
【解决方案2】:

您可以简单地运行一个while循环并这样做:

#include <iostream>
using namespace std;

int main()
{
    const int limit = 45;
    int bag;
    string yn = "yes";
    while(yn == "yes")
    {
        cout << "Please type your number here: ";
        cin >> bag;
        if (limit < bag)
        {
            cout << "You passed the limit." << endl;
        }
        else if (limit == bag)
        {
            cout << "Just enough." << endl;
        }
        else if (limit > bag)
        {
            cout << "You got space." << endl;
        }
        cout << "Do you want to try another number?" << endl;
        cin >> yn;
        cout << endl;
    }
}

【讨论】:

  • 谢谢 Muhammad Noman,非常感谢您的回答。我还没有想到你的方法,所以我很感谢你的反馈
  • 如果出现输入错误怎么办? yn 永远不会改变,你会被困在一个循环中。您需要检查输入是否有错误
猜你喜欢
  • 2016-12-25
  • 2017-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多