我在 Visual Studio 2019 上用 C 制作了一个控制台游戏。我制作了一个打印变量的代码。
它仅在 value 大于 10 时才起作用。
如果变量的值小于 10,它会打印 10 而不是 1, 2 而不是 20 ... 9 而不是 90。
我不知道如何解决这个问题。
这是我的代码
#include<stdio.h>
#include<windows.h>
int a =20;
void gotoxy(int x, int y) { //cursor goes to x, y
COORD CursorPosition = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPosition);
}
void HideCursor() {
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
}
void count() {
char c = _getch();
switch (c) {
case 'p': a++; break;
case 'o': a--; break;
}
}
int main() {
system("mode con cols=10 lines=10");
system("cls");
HideCursor();
while (1) {
gotoxy(5, 5);
count();
printf("%d", a);
}
}
Run Code Online (Sandbox Code Playgroud)
这是因为如果a变成一位数,您不会覆盖第二个数字。您需要覆盖所有以前写入的数字,例如通过设置字段宽度:
// Print right-justified numbers, with empty spaces for one or two digit numbers
printf("%3d", a);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |