![](/img/trans.png)
[英]C. the prints don't come out right, so far haven't been able to figure out why
[英]Loading in C doesn't come out right
下面是我的簡單代碼,我在其中嘗試加載 animation,但 output 似乎與預期無關。 請幫助我指出我的錯誤,因為我是 C 的新手。
#include <stdio.h>
#include <windows.h>
LO();
main(){
LO();
}
LO(){
char ad = '-';
int n = 1;
while (n<10){
printf(" %c >", ad);
Sleep(1000);
ad += n*'-';
n++;
}
}
output:
- > Z > ┤ > ; > ∩ > ╨ > ▐ > > ü >
我所期望的:
->
然后清屏
-->
然后清屏
--->
直到特定時間。
然后清屏
C不是Python。 它沒有n *
字符串連接。
這一行:
ad += n*'-';
ad 最初的數字 ASCII 值為 45。 文字 '-' 也是 45。您希望將 n * '-' 連接到字符串,但您只是在進行數字計算。
因此,當n == 1
時,您將 45 添加到 45,即 ASCII 'Z',這就是您在 output 中看到的內容。
當您想在 C 中連接時,您可以使用strcpy
, strcat
。
一個相當老套的方法是 printf 的 %.*s 格式說明符。
const char *fullline = "---------------------------------";
for (int i = 1; i < 10; i++)
printf("%.*s\n", i, fullline);
printf 在 i 個字符后切斷整行。
一種方法是使用for
循環:
void LO(void)
{
int n = 1;
while(n < 10)
{
for(int i = 0; i < n; i++)
printf("-");
printf(">");
Sleep(1000);
printf("\r");
fflush(stdout);
n++;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.