目前正在開發將回圈直到用戶輸入“n”的附加程式。
它將生成兩個亂數并顯示給用戶添加它們。然后用戶將輸入答案,程式檢查答案是對還是錯。
我的代碼運行良好,但是我需要幫助下面的代碼來計算正確和錯誤的答案。
我什么都不累,因為我不知道該怎么做。
/******************************************************************************
Basic Template for our C Programs.
STRING
*******************************************************************************/
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <string> // String managment funtions.
#include <iostream> // For input and output
#include <cmath> // For math functions.
#include <math.h>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////////
int main()
{
srand(time(0));
string keepgoing;
do
{
const int minValue = 10;
const int maxValue = 20;
int y = (rand() % (maxValue - minValue 1)) minValue;
// cout<< " the random number is y "<< y << endl;
int x = (rand() % (maxValue - minValue 1)) minValue;
// cout<< " the random number is x "<< x << endl;
cout << " what is the sum of " << x << " " << y << " =" << endl;
int answer;
cin >> answer;
if (answer == (x y))
{
cout << "Great!! You are really smart!!" << endl;
}
else
{
cout << "You need to review your basic concepts of addition" << endl;
}
cout << "Do you want to try agian [enter y (yes) or n (no) ]";
cin >> keepgoing;
} while (keepgoing == "y");
return 0;
}
uj5u.com熱心網友回復:
為了讓生活更輕松,讓我們使用兩個變數:
unsigned int quantity_wrong_answers = 0U;
unsigned int quantity_correct_answers = 0U;
(這應該放在do宣告之前。)
當您檢測到正確答案時,請增加以下變數之一:
if (answer = (x y))
{
quantity_correct_answers;
}
else
{
quantity_wrong_answers;
}
在從 回傳之前main,您可以列印統計資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/346461.html
標籤:C
上一篇:c 從std::vector<std::tuple<int,float>>獲取std::vector<int>
下一篇:C 嵌套while回圈只運行一次
