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

标签 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=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");
 }
 }

我想在白帽数量等于 move 之后打印 T。

最佳答案

使用长度格式说明符

printf("%*s%s\n", tmove, " ", "T");

另外,请注意每个 switch case 都可以这样写

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;
}

这是 maximum = 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

关于c - 在c中用变量格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27580872/

相关文章:

c - 我怎么知道我的 .so 库的足迹?

c - 是否可以在每条指令后转储内存?

c - 尝试将 Haskell 函数导出到 C 时出现元组 "cannot be marshalled in a foreign call"

string - 在 Scala 中使用 f 字符串插值器强制指定小数点分隔符

C 具有结构的双向链表

c - 使用 munmap 时回收更高的页面

java - Android - 如何用另一个字符串替换部分字符串?

c - 如何逐个字母地考虑字符串并将它们与 if 函数进行比较?

java - String.replace 和同一方法多次调用

java - 交换字符串中 2 个符号的出现次数