Pluto.164 2021-11-23 00:15 采纳率: 100%
浏览 31
已结题

很简单的一个点但没明白

让学生自己输入两个加数,通过这两个加数给学生出一道加法运算题,如果学生输入答案正确,则显 示“Right!”否则提示重做,显示“Not correct! Try again!”,重做机会最多给三次,如果三次仍未做对,则显示“Not correct,You have tried three times! Test over!”,程序结束。

#include <iostream>
using namespace std;

int main() {
    float x, y, t;
    cout << "input two numbers,give me the sum of them ";
    cin >> x >> y;
    cout << x << "+" << y << "=?" << endl;
    cout << "input your answer ";
    cin >> t;
    for (int i = 0; t != x + y; i++) {

        cout << "Not correct! Try again!";
        cin >> t;

        if (i == 3)
            cout << "Not correct,You have tried three times! Test over!";
        break;


    }
    if (t == x + y)
        cout << "Right!";





    return 0;
}

结果输入错误答案一次之后程序就结束了,觉得逻辑没问题

  • 写回答

1条回答 默认 最新

  • 技术专家团-Bamboo 2021-11-23 00:19
    关注
    if (i == 3){
                cout << "Not correct,You have tried three times! Test over!";
                break;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月1日
  • 已采纳回答 11月23日
  • 创建了问题 11月23日