首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >教基础数学的小文本游戏

教基础数学的小文本游戏
EN

Code Review用户
提问于 2017-07-18 15:30:11
回答 1查看 119关注 0票数 2

这是赋值的代码。

还有我可以避免使用的额外代码吗?我目前正在学习C++,所以我只限于我的资源,但我确实觉得我给了自己额外的工作。有什么建议可以让我在这方面更有效率吗?

选择就是菜单

在输入中回答

退出不在菜单选项之外。它是do-while循环的一部分。

不完全理解时间命令

随机数发生器

用于保持num1可被num6除除。

分配给每个操作的变量

抱歉对代码没有任何评论。

代码语言:javascript
复制
#include<iostream>
#include <cstdlib> 
#include <ctime>

using namespace std;


int main() {

int choice,
answer;

const int quit = 5;


unsigned seed = time(0);
srand(seed);



do
{

int num1 = rand() % 10,
    num2 = rand() % 10,
    num3 = rand() % 11,
    num5 = rand() % 9,
    num4 = num5 * (rand() % 10 + 1),
    num6 = rand() % 10;

while (num6 > num1)
{
    num6 = rand() % 10;
}



int answer1 = num1 + num2,
    answer2 = num1 - num6,
    answer3 = num3 * num1,
    answer4 = num4 / num5;


cout << "Menu\n"
    << "1. Addition problem\n"
    << "2. Subtraction problem\n"
    << "3. Multiplication problem\n"
    << "4. Division problem\n"
    << "5. Quit this program\n\n"
    << "Enter your choice (1-5): ";
cin >> choice;

while (choice > 5 || choice < 1)
{
    cout << "The valid choices are 1, 2, 3, 4, and 5. Please choose: ";
    cin >> choice;
    cout << endl;
}
    switch (choice)
    {
    case  1: cout << endl;
        cout << "  " << num1 << endl;
        cout << "+ " << num2 << endl;
        cout << " ---" << endl;
        cout << "  ";
        cin >> answer;

        if (answer == answer1)
        {
            cout << endl;
            cout << "Congratulations! That's right." << endl;
            cout << endl;
        }
        else
        {
            cout << endl;
            cout << "Sorry, the correct answer is " << answer1 << "." << endl;
            cout << endl;
        }
        break;
    case  2: cout << endl;
        cout << "  " << num1 << endl;
        cout << "- " << num6 << endl;
        cout << " ---" << endl;
        cout << "  ";
        cin >> answer;

        if (answer == answer2)
        {
            cout << endl;
            cout << "Congratulations! That's right." << endl;
            cout << endl;
        }
        else
        {
            cout << endl;
            cout << "Sorry, the correct answer is " << answer2 << "." << endl;
            cout << endl;
        }
        break;
    case  3: cout << endl;
        cout << "  " << num1 << endl;
        cout << "* " << num3 << endl;
        cout << " ---" << endl;
        cout << "  ";
        cin >> answer;

        if (answer == answer3)
        {
            cout << endl;
            cout << "Congratulations! That's right." << endl;
            cout << endl;
        }
        else
        {
            cout << endl;
            cout << "Sorry, the correct answer is " << answer3 << "." << endl;
            cout << endl;
        }
        break;
    case  4: cout << endl;
        cout << "  " << num4 << endl;
        cout << "/ " << num5 << endl;
        cout << " ---" << endl;
        cout << "  ";
        cin >> answer;

        if (answer == answer4)
        {
            cout << endl;
            cout << "Congratulations! That's right." << endl;
            cout << endl;
        }
        else
        {
            cout << endl;
            cout << "Sorry, the correct answer is " << answer4 << "." << endl;
            cout << endl;
        }
        break;
    }

} while (choice != quit);
{
cout << "Thank you for using math tutor!" << endl;
cout << endl;
}

system("PAUSE");
return 0;

}
EN

回答 1

Code Review用户

回答已采纳

发布于 2017-07-18 17:41:22

  1. 修复你的压痕。大多数情况下,当您打开一个块时,您会添加一个缩进级别,但不幸的是,并不总是这样。坚持这样做,您的代码就会更加可读性。
  2. 永远不要使用using namespace std;。是的,无论结果如何,书作者常常被出版商强迫牺牲在简洁的祭坛上,但你不是。读了"为什么“使用命名空间性病;”被认为是不好的做法?“。
  3. 是的,您可以在一个函数中使用一个空行来以图形方式分隔执行不同事情的块。但是不要使用多行,并考虑是否将其提取到一个命名良好的函数中可能会使事情更加可读性,或者至少允许您重用代码。您真正应该提取的一个函数是这个,它将被所有挑战调用:静态无效do_challenge(char op,int n1,int n2,int n2,int结果){ std::cout << "\n“<< n1 <<‘\n << op <<’<< n2 << "\n -\n;int应答;if (std:cin >>应答) if (回答==结果)::cout <<”\n恭喜!是的。\n\n“;否则std::cout <<”\n对不起,正确的答案是“<< result <<”.\n\n;}
  4. 如果您可以简单地内联它的初始化,那么就不要添加一个新变量,除非它使事情更易读。应该是:srand(时间(0));
  5. 除非您真的需要手动冲洗,否则不要使用std::endl,因为冲水很昂贵,所以使用'\n'代替。请记住,std::cinstd::cout是相互关联的。
  6. 不要在一行中流多个字符串和字符文本,将它们连接起来,并流一个字符串-文字。只有空格(包括换行符)分隔的两个字符串文本由编译器连接。
  7. 余数的第二个参数不一定是常数。
  8. 你知道num5可能是零,导致除以零,这是未定义的行为?
  9. 推迟确定挑战的参数,直到你知道你需要什么。否则,大多数都是无用的。
  10. 不要以为输入会成功。试试看。
  11. 如果需要等待输入,只需从std::cin读取,这是可移植的、安全的和高效的,避免使用std::system()。std::cin.get();
  12. return 0;main()是隐式的。
  13. 关于“抱歉没有对代码的评论”,我实际上并没有错过它们。没有太多的理由可以解释,至少你没有重述你的代码。
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/169568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档