如何在if语句中比较两个变量?

时间:2019-03-03 23:54:53

标签: c++

我正在创建一个程序,用户在该程序中尝试猜测两个骰子必须滚动多少次才能达到100的总和。

我已经完成了循环,但是现在我停留在希望程序比较两个变量的地步。我已经用谷歌搜索,但是关于这个问题没有任何进展。

关于代码,这是我所拥有的:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
const int min_value=1;
const int max_value=6;
int die1,die2,sum,totalRolls, totalSum, prediction;
unsigned seed = time(0);
srand(seed);
sum=0;
totalRolls=0;
totalSum=0;
cout << "How many rolls will it take to reach a total of 100?\n";
cin >> prediction;

while (totalSum <= 100)
{
    cout << "Rolling the die\n";
    die1=(rand() % (max_value - min_value + 1) + min_value); 
    die2=(rand() % (max_value - min_value + 1) + min_value);

    cout << die1 << endl;
    cout << die2 << endl;
    sum=die1+die2;
    totalSum+=sum;
    cout << "Your current total is " << totalSum << endl;
    totalRolls++;
    cout << "Last roll number = " << totalRolls << endl;

}

if (totalRolls <= 5)
    {
    cout << "Amazing!\n";   
    }   

else if (totalRolls <=10)
    {
    cout << "Good\n";
    }
else if (totalRolls <= 15)
    {
    cout << "Okay\n";
    }
else if (totalRolls <= 20)
    {
    cout << "Try harder\n";
    }
system("Pause");
return 0;

}

老实说,我只是不知道如何将预测的总卷数与实际花费的卷数进行比较。

0 个答案:

没有答案