循环运行,直到用户按下键 [英] Loop Run Until User Presses Key

查看:162
本文介绍了循环运行,直到用户按下键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用什么代码来执行此逻辑:


  • 继续循环,直到用户按下所按下的键,此时程序将暂停.
  • 如果用户再次按下某个键,则继续循环.
  • 如果用户再次按下一个键,然后完全停止循环(即退出程序).
  • What code should I use to execute this logic:


    • Continue to loop until the user presses a key pressed, at which point the program will pause.
    • If the user presses a key again, then resume the loop.
    • If the user presses a key again, then stop the loop completely (i.e., quit the program).
    • 推荐答案

      不同的线程.主线程将读取按键,并将t的值从0增加到更高.

      stdlib中还有另一个检测按键的功能,我无法回忆起该功能不要等待按键...
      run the process in a different thread. main thread will read the key stroke and increase the value from t from 0 to higher.

      there is another function in stdlib that detect keystroke i cannot recall that function dont wait for key stroke...
      void runningProcess()
      { 
       while(1)
       {
          if(t==1)   //t is a global variable;
          {
           while(1)
           {
            if(t==2)
            {
             break;
            } 
           }
          }
          else if(t==3)
          {
            return;
          }
       }
      }


      void module()
      {
          int notexit = 2;
          while(notexit)
          {
              if(kbhit())
              {
                  if(notexit != 1)
                      getch();
                  notexit--;
              }
          }
      }


      这篇关于循环运行,直到用户按下键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆