【问题标题】:how do you run two do while independently in C你如何在C中独立运行两个do
【发布时间】:2013-08-03 07:40:58
【问题描述】:

我试图创建类似于“迷失在迁移中”的东西作为我的决赛项目,但我在随机化和计时器方面遇到了麻烦

我如何在彼此独立的情况下运行两个 do

定时器不应该消失,必须一直运行到时间到,必须独立运行

随机化不得影响计时器

错误 输入方向键不显示结果

while (timer) 一次然后去 do while (randomization) 并且会一直循环在 "randomize" 并且不会回到 while (timer)

while (timer) 受到影响 bu getch() 和 getche() 暂停它

 #include<stdio.h>
 #include<conio.h>
 #include<stdlib.h>
 #include<windows.h>
 #include<time.h>

//******************************************//
//       DEFAULT BUILT-IN CLOCK             //
//******************************************//

void Wait(int seconds)
{
   clock_t end wait;
   endwait = clock () + seconds * CLK_TCK;
   while (clock() < end wait) {}
}

//***********************************************************//
//                    DIRECTIONAL KEYS                       //
//***********************************************************//

#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80

int rdtsc()
{
as m volatile("rdtsc");
}

int main()
{
    char ans;
    int image;
    int loop=1;    
    int correct=0, total=0;
    int sec=0, min=0;

//***********************************************************//
//                       TIMER                               //
//***********************************************************//
int x=1;
while(1==1)
{
    Wait(1);
    sec++;
    if(sec==46)
    {
           loop=0;
    }

    printf("%i:%i\n\n", min, sec);

//***********************************************************//
//                RANDOMIZED IMAGES                          //
//***********************************************************//


fflush(st din);
srand(rdtsc());
image=rand()%4;

do
{

    if(image==0)
    {
    printf(">>IMAGE 1 CORRECT LEFT<<");    
    ans=getche();                    
                    if(ans==LEFT)
                    {        
                    printf("\n\ncorrect!");           
                    }
                    else
                    if(ans!=LEFT)
                    {
                    printf("\n\nwrong!");
                    }   
    }
    else
    if(image==1)
    {
    printf(">>IMAGE 2 CORRECT UP<<");
    ans=getche();
                    if(ans==UP)
                    {         
                    printf("\n\ncorrect!");           
                    }
                    else
                    if(ans!=UP)
                    {
                    printf("\n\nwrong!");
                    }   
    }
    else
    if(image==2)
    {
    printf(">>IMAGE 3 CORRECT DOWN<<");
    ans=getche();
                    if(ans==DOWN)
                    {
                    printf("\n\ncorrect!");                     
                    }
                    else
                    if(ans!=DOWN)
                    {
                    printf("\n\nwrong!");
                    }        
    }
    else
    if(image==3)
    {
    printf(">>IMAGE 4 CORRECT RIGHT<<");
    ans=getche();
                    if(ans==RIGHT)
                    {      
                    printf("\n\ncorrect!");           
                    }
                    else
                    if(ans!=RIGHT)
                    {
                    printf("\n\nwrong!");
                    }           
    }    
getch();
system("cls");
}
while(loop==1);

}

getch();
}

【问题讨论】:

    标签: timer while-loop do-while arrow-keys


    【解决方案1】:
    • 为您尝试完成的两个任务中的每一个创建一个状态机。
    • 编写代码以使用 switch 语句实现每个状态机。
    • 将它们按顺序放置在外循环中。

      for (int i = 0; i

      开关(状态1) { 案例0: // 为任务 1 做任何你需要的事情 }

      开关(状态2) { 案例0: // 为任务 2 做任何你需要的事情 }

      }

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 2020-03-31
      • 2023-03-28
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多