在c中使用变量格式化字符串

在c中使用变量格式化字符串,c,string,C,String,我在用c写。我想让一个字符在运行时间时在屏幕上移动。我正在考虑使用printf(),它可以在指定数量的空格后使用%s打印屏幕 我正在将此数字保存在变量中如何使用变量非常量值的%s 代码是 #include <stdio.h> #include <stdlib.h> int main() { int it=0; int tr; printf("T \n"); srand(time(NULL)); while(it != 80 ) { tr=r

我在用c写。我想让一个字符在运行时间时在屏幕上移动。我正在考虑使用
printf()
,它可以在指定数量的空格后使用
%s
打印屏幕

我正在将此数字保存在变量中如何使用变量非常量值的
%s

代码是

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int it=0;
  int tr;
  printf("T \n");
  srand(time(NULL));
  while(it != 80 )
  {
   tr=rand()%3+1;
   switch(tr)
   {
       int tmove=it;
       case 1 :{
           if(80-it>1)
           {
               tmove+=1;
               it+=1;
           }
           else it=80;
          break;}
       case 2 :{
           if(80-it>2)
            {
                tmove=+2;
                it+=2;
            }
           else it=80;
          break;}
       case 3 :{
           if(80-it>3)
           {
               tmove+=3;
               it+=3;
           }
           else it=80;
          break;}
            default:break;
   }
   printf("%s","T");
 }
 }
#包括
#包括
int main()
{
int it=0;
int tr;
printf(“T\n”);
srand(时间(空));
而(它=80)
{
tr=rand()%3+1;
开关(tr)
{
int-tmove=it;
案例1:{
如果(80 it>1)
{
tmove+=1;
it+=1;
}
否则它=80;
中断;}
案例2:{
如果(80 it>2)
{
t移动=+2;
it+=2;
}
否则它=80;
中断;}
案例3:{
如果(80 it>3)
{
tmove+=3;
it+=3;
}
否则它=80;
中断;}
默认:中断;
}
printf(“%s”、“T”);
}
}

我想使T在白帽数等于移动后打印。

使用长度格式说明符

printf("%*s%s\n", tmove, " ", "T");
另外,请注意,每个开关盒都可以这样写

if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else
    it = 80;
因此,您实际上不需要
开关
,只需编写

tr = rand() % 3 + 1;
if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else if (it > 3) /* for the default case of the switch */
    it = 80;
因此,整个程序转换为

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s", tmove, " ", "T");
    }
    return 0;
}
判断代码中的第一个
printf
,以及
tmove
总是在增加这一事实,我想说第二个
printf
中也需要换行符,输出看起来很有趣,是什么

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}
还有一个技巧,将
3
设置为一个变量,以便在需要时修改它,而无需到处更改它

int main()
{
    int it = 0;
    int tr;
    int maximum = 3;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % maximum + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > maximum)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}
这是
最大值=3

T
  T
    T
      T
       T
         T
            T
             T
               T
                T
                 T
                  T
                   T
                     T
                      T
                         T
                            T
                              T
                               T
                                  T
                                     T
                                        T
                                         T
                                          T
                                           T
                                            T
                                              T
                                               T
                                                 T
                                                   T
                                                      T
                                                        T
                                                          T
                                                             T
                                                               T
                                                                 T
                                                                   T
                                                                    T
                                                                     T
                                                                      T
                                                                       T
                                                                        T
                                                                          T
                                                                            T
                                                                               T
                                                                               T

使用长度格式说明符

printf("%*s%s\n", tmove, " ", "T");
另外,请注意,每个开关盒都可以这样写

if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else
    it = 80;
因此,您实际上不需要
开关
,只需编写

tr = rand() % 3 + 1;
if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else if (it > 3) /* for the default case of the switch */
    it = 80;
因此,整个程序转换为

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s", tmove, " ", "T");
    }
    return 0;
}
判断代码中的第一个
printf
,以及
tmove
总是在增加这一事实,我想说第二个
printf
中也需要换行符,输出看起来很有趣,是什么

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}
还有一个技巧,将
3
设置为一个变量,以便在需要时修改它,而无需到处更改它

int main()
{
    int it = 0;
    int tr;
    int maximum = 3;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % maximum + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > maximum)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}
这是
最大值=3

T
  T
    T
      T
       T
         T
            T
             T
               T
                T
                 T
                  T
                   T
                     T
                      T
                         T
                            T
                              T
                               T
                                  T
                                     T
                                        T
                                         T
                                          T
                                           T
                                            T
                                              T
                                               T
                                                 T
                                                   T
                                                      T
                                                        T
                                                          T
                                                             T
                                                               T
                                                                 T
                                                                   T
                                                                    T
                                                                     T
                                                                      T
                                                                       T
                                                                        T
                                                                          T
                                                                            T
                                                                               T
                                                                               T

使用长度格式说明符

printf("%*s%s\n", tmove, " ", "T");
另外,请注意,每个开关盒都可以这样写

if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else
    it = 80;
因此,您实际上不需要
开关
,只需编写

tr = rand() % 3 + 1;
if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else if (it > 3) /* for the default case of the switch */
    it = 80;
因此,整个程序转换为

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s", tmove, " ", "T");
    }
    return 0;
}
判断代码中的第一个
printf
,以及
tmove
总是在增加这一事实,我想说第二个
printf
中也需要换行符,输出看起来很有趣,是什么

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}
还有一个技巧,将
3
设置为一个变量,以便在需要时修改它,而无需到处更改它

int main()
{
    int it = 0;
    int tr;
    int maximum = 3;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % maximum + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > maximum)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}
这是
最大值=3

T
  T
    T
      T
       T
         T
            T
             T
               T
                T
                 T
                  T
                   T
                     T
                      T
                         T
                            T
                              T
                               T
                                  T
                                     T
                                        T
                                         T
                                          T
                                           T
                                            T
                                              T
                                               T
                                                 T
                                                   T
                                                      T
                                                        T
                                                          T
                                                             T
                                                               T
                                                                 T
                                                                   T
                                                                    T
                                                                     T
                                                                      T
                                                                       T
                                                                        T
                                                                          T
                                                                            T
                                                                               T
                                                                               T

使用长度格式说明符

printf("%*s%s\n", tmove, " ", "T");
另外,请注意,每个开关盒都可以这样写

if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else
    it = 80;
因此,您实际上不需要
开关
,只需编写

tr = rand() % 3 + 1;
if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else if (it > 3) /* for the default case of the switch */
    it = 80;
因此,整个程序转换为

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s", tmove, " ", "T");
    }
    return 0;
}
判断代码中的第一个
printf
,以及
tmove
总是在增加这一事实,我想说第二个
printf
中也需要换行符,输出看起来很有趣,是什么

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}
还有一个技巧,将
3
设置为一个变量,以便在需要时修改它,而无需到处更改它

int main()
{
    int it = 0;
    int tr;
    int maximum = 3;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % maximum + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > maximum)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}
这是
最大值=3

T
  T
    T
      T
       T
         T
            T
             T
               T
                T
                 T
                  T
                   T
                     T
                      T
                         T
                            T
                              T
                               T
                                  T
                                     T
                                        T
                                         T
                                          T
                                           T
                                            T
                                              T
                                               T
                                                 T
                                                   T
                                                      T
                                                        T
                                                          T
                                                             T
                                                               T
                                                                 T
                                                                   T
                                                                    T
                                                                     T
                                                                      T
                                                                       T
                                                                        T
                                                                          T
                                                                            T
                                                                               T
                                                                               T

您可以通过添加
#include
来使用函数
clrsc()
,并创建一个变量
nb
进行左移,然后您可以在变量
名称中打印名称,例如

printf("%*s",nb,name);
您可以将所有这些放在一个循环中,每次清除屏幕时,增加
nb
变量,最后打印
名称
,您可以看到它在移动

对于那些使用linux的人


要使用透明屏幕,您需要添加
#include
并使
系统(“透明屏幕”)
您可以通过添加
#include
来使用函数
clrsc()
,并创建一个变量
nb
,用于左移,然后您可以在变量
name
中打印名称

printf("%*s",nb,name);
您可以将所有这些放在一个循环中,每次清除屏幕时,增加
nb
变量,最后打印
名称
,您可以看到它在移动

对于那些使用linux的人


要使用透明屏幕,您需要添加
#include
并使
系统(“透明屏幕”)
您可以通过添加
#include
来使用函数
clrsc()
,并创建一个变量
nb
,用于左移,然后您可以在变量
name
中打印名称

printf("%*s",nb,name);
您可以将所有这些放在一个循环中,每次清除屏幕时,增加
nb
变量,最后打印
名称
,您可以看到它在移动

对于那些使用linux的人


要使用透明屏幕,您需要添加
#include
并使
系统(“透明屏幕”)
您可以通过添加
#include
来使用函数
clrsc()
,并创建一个变量
nb
,用于左移,然后您可以在变量
name
中打印名称

printf("%*s",nb,name);
您可以将所有这些放在一个循环中,每次清除屏幕时,增加
nb
变量,最后打印
名称
,您可以看到它在移动

对于那些使用linux的人

要使用透明屏幕,您需要添加
#include<