- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在为我的俱乐部编写程序。要求用户在 2 分钟内回答 10 个问题。我正在为每个问题使用函数,并在用户回答完当前问题后调用下一个函数。如何在显示问题的同时在顶部显示剩余时间?
我试过循环来控制我的时间。但它不会在顶部显示剩余时间并同时显示问题,因为循环需要执行直到条件为假
Question1()
{
countdown();
cout<<"Question 1 out of 10"<<endl<<endl;
cout<<" Where Is The First Indoor Bowling Lane Built? "<<endl;
cout<<"A. New York City"<<endl;
cout<<"B. Berlin"<<endl;
cout<<"C. Ohio"<<endl;
cout<<"D. Japan"<<endl;
cout<<"Your answer: ";
cin>>ans1;
Question2();
}
void countdown()
{
while(timer>=0)
{
cout<<"Time remaining: "<<timer<<endl;
Sleep(1000);
timer--;
system("cls");
}
}
预计时间会在我的问题上进行倒计时,但问题只会在设定的时间结束前显示。
最佳答案
我回想起 MS-DOS 的日子,记得有一个函数 _kbhit
由 <conio.h>
提供在 Windows 中。它不是可移植的,但也许你不在乎。这是我编写的一个函数,将使用 _kbhit
轮询按键。它很粗糙,但也许这就是您要找的东西。
int GetInputWithTimeout(int count, int timeout)
{
int answer = -1;
const int poll_rate = 10;
int poll_cycle = 0, last_timeout = 0;
do {
// poll for keypress and store answer if valid
if (_kbhit())
{
int input = _getch();
while (_kbhit()) _getch(); // discard control sequences
input = tolower(input);
if (input >= 'a' && input < 'a' + count)
{
answer = input - 'a';
}
}
// display time remaining if changed
if (last_timeout != timeout) {
last_timeout = timeout;
std::cout << "\rTime remaining: " << timeout << " " << std::flush;
}
// perform small sleeps for a potentially wildly inaccurate, but responsive delay
if (timeout > 0)
{
Sleep(1000 / poll_rate);
poll_cycle = (poll_cycle + 1) % poll_rate;
if (poll_cycle == 0) --timeout;
}
} while (timeout > 0 && answer == -1);
std::cout << std::endl;
return answer;
}
因此您可以使用此函数,传递所需数量的测验输入。建议的用法是这样的:
struct Question {
std::string question;
std::vector<std::string> answers;
int correct_answer;
int timeout_seconds = 10;
};
bool AskQuestion(int number, const Question& question)
{
std::cout << "Question " << number << ": " << question.question << std::endl;
int count = static_cast<int>(question.answers.size());
for (int i = 0; i < count; i++)
{
std::cout << " " << char('A' + i) << ": " << question.answers[i] << std::endl;
}
int answer = GetInputWithTimeout(count, question.timeout_seconds);
return answer == question.correct_answer;
}
您会注意到我在结构中表示了一个测验问题。因此,您可以像这样设置和运行整个测验:
int main()
{
std::vector<Question> questions = {
{
"Where Is The First Indoor Bowling Lane Built?",
{
"New York City",
"Berlin",
"Ohio",
"Japan"
},
0, // I have no idea, so just guessed it's new york
},
{
"What is the airspeed velocity of an unladen swallow?",
{
"10km/h",
"20km/h",
"30km/h",
"40km/h",
"What do you mean -- an african or european swallow?"
},
4,
},
};
int num_questions = static_cast<int>(questions.size());
int num_correct = 0;
for (int q = 0; q < num_questions; q++)
{
num_correct += AskQuestion(q, questions[q]);
std::cout << std::endl;
}
std::cout << "Final score: " << num_correct << " of " << num_questions << std::endl;
return 0;
}
呃,糟糕,我有点为你写了整个程序。它并不漂亮,但希望它至少能教给你一些东西。
关于c++ - 如何让定时器在后台保持运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57068293/
从 0 开始搭建一套后台管理系统,成本巨大,所以都会选择一套成熟的组件库,基于此,再堆叠业务逻辑。我们公司的组件库基于 Ant Design。Ant Design 包含一套完整的后台解决方案,不仅
在我的 IOS 应用程序中,我有一个标记为 retain 的 NSDate* 属性 当我的应用程序再次激活时,属性值已被释放。 我是否误解了属性和内存管理的工作原理,我该如何防范? 最佳答案 很明显,
我有一个使用 BackgroundWorker 组件的示例 WinForms 应用程序。它工作正常,但是当我点击 Cancel 按钮取消后台线程时,它并没有取消线程。当我点击 Cancel 按钮调用
我目前正在开发一个应用程序,该应用程序在启动时会对服务器执行 ping 操作,该服务器会为每个连接的设备返回一个唯一标识符。设备每 5 秒从服务器检索另一页以获取一组不同的数据。这个唯一的 ID 可以
我正在开发一个应用程序,当它通过主页按钮在后台按下时,计时器应该启动,当应用程序返回前台并且计时器已经过了一定时间时,应该是执行。 我的问题是 当我的应用程序转到背景/前景? 是否有特殊的方法或其他技
我有 map View ,其中几乎没有 MKPointAnnotation。 一切正常,但是, View 的 MKPoiintAnnotation 的“背景”是“不可见的”,因此不是很“可见”。 我想
我在 iOS 中开发广告数据应用程序。我的应用程序广告数据在前台很好。但我想在 ios 后台宣传信标数据。我设置了背景外设设置。和广告数据 advertisingData = [CBAdvertise
如果我有一组操作,我想根据特定条件在后台工作程序中运行,例如,我有 10 个条件 if(a) BackgroundWorker doA = new backgroundworker() if(
我想独立运行一个函数。从我调用的函数中,我想在不等待其他函数结束的情况下返回。 我试过用 threadind,但这会等待,结束。 thread = threading.Thread(target=my
我想在用户在线时立即执行一些任务,即使他在后台也是如此。我正在使用 Reachability 类来检查互联网。但是当我在后台时,这个类没有通知我。我知道有人早些时候问过这个问题,但没有找到任何解决方案
我在后台播放文本转语音时出现间歇性(哎呀!)问题,由 Apple Watch 触发。我已经正确设置了后台模式、AVSession 类别和 WatchKitExtensionRequest 处理程序。
我有一个相当复杂的程序,所以我不会在这里转储整个程序。这是一个简化版本: class Report { private BackgroundWorker worker; public
我有一个任务在 backgroundworker 中运行。单击开始按钮,用户将启动该过程,并获得一个取消按钮来取消处理。 当用户点击取消时,我想显示一个消息框“进程尚未完成,你想继续吗”。 这里我希望
我有一个按以下方式编码的脚本。我想将它作为后台/守护进程运行,但是一旦我启动脚本,如果我关闭它从程序运行的终端窗口终止。我需要做什么来保持程序运行 loop do pid = fork do
我正在制作一个使用 ActivityRecognition API 在后台跟踪用户 Activity 的应用,如果用户在指定时间段(例如 1 小时)内停留在同一个地方,系统就会推送通知告诉用户去散步.
当尝试使用 URLSession 的 dataTaskPublisher 方法发送后台请求时: URLSession(configuration: URLSessionConfiguration.ba
当我编译这段代码时,我得到了他的错误,对象引用设置为null,错误位置在Dowork中,argumenttest.valueone = 8; public partial class Form1 :
有什么方法可以使用最小化或不活动的应用程序吗?我可以打开我的应用程序,然后打开并使用另一个应用程序,然后按一个按钮来激活我的程序吗? 例如,打开我的应用程序,打开 Safari,按下按钮(F1 或任何
我的具体要求是一个在后台运行的应用程序,被通知显示器即将进入休眠状态或者设备已经或即将达到空闲超时 - 然后唤醒并执行一些(简短的)一段代码。 我在这里找到了有关应用程序被置于后台或暂停的通知的引用:
我有一个 LSUIElement 设置为 1 的应用程序。它有一个内置编辑器,因此我希望该应用程序在编辑器打开时出现在 Cmd+Tab 循环中。 -(void)stepIntoForegrou
我是一名优秀的程序员,十分优秀!