我无法弄清楚如何使这项工作:
cout << "Start";
cin.ignore(); // Starts the loop.
int x = 1;
while ( x == 1 )
{
if ( x != 2 )
{
cout << "\nThe screen counts down from 10.";
x = x + 1;
Sleep ( 10000 ); // Gives the user 10 seconds to make x = 3 and disable the bomb.
cout << "\nEnter the correct number to disable the bomb: ";
cin >> x; // Sleep makes x = 2 after 10 seconds if user doesn't make x = 3.
}
else if ( x == 2 )
cout << "\nThe bomb went off and you died.";
else if ( x == 3 )
cout << "\nYou successfully disabled the bomb!";
}
运行此程序后,输出为:
“屏幕倒数从10开始 .
10 seconds pass.
输入正确的号码以禁用炸弹:“
这个程序运行的方式是让用户cin >> x,而等式x = x 1等待执行Sleep . 但是,似乎该程序在继续下一个语句之前等待Sleep完成 .
如何在执行Sleep时继续循环 . 这个想法是,如果用户没有足够快地输入3来禁用炸弹,那么由于睡眠,x将为2,并且系统将跳转到while语句的(x == 2)部分 .
我仍然是编程的新手,但我正在开始我的第一场比赛 . 我想包括这一部分,但我不知道这个任务的正确命令可能吗?或者我错过了什么?