我需要一些C++代码方面的帮助。我正在尝试制作一个程序,让单词向上和向下在屏幕上显示。我想让它这样做三次。我的程序是成功的,单词做了我想让他们做的事。然而,它没有做三次。我把它放在for循环中,所以我不确定出了什么问题。我在学习,所以我只需要帮助来理解我做错了什么。这是我的代码:
#include <iostream>
#include<windows.h>
using namespace std;
int main()
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
COORD posUp = {5,25};
COORD posDown = {68,1};
SetConsoleCursorPosition(screen, posUp);
cout<<"Up"<<endl;
SetConsoleCursorPosition(screen, posDown);
cout<< "Down"<<endl;
Sleep(400);
for(int comeback=1; comeback<=3; comeback++) {
while(posUp.Y > 1 && posDown.Y < 24) {
SetConsoleCursorPosition(screen,posUp);
cout<<" "<<endl;
posUp.Y--;
SetConsoleCursorPosition(screen, posUp);
cout<<"Up"<<endl;
SetConsoleCursorPosition(screen, posDown);
cout<<" ";
posDown.Y++;
SetConsoleCursorPosition(screen, posDown);
cout<<"Down"<<endl;
Sleep(100);
}
SetConsoleCursorPosition(screen, posUp);
cout<<" "<<endl;
SetConsoleCursorPosition(screen, posDown);
cout<< " "<<endl;
Sleep(100);
}
return 0;
}
我看起来像是在while循环中递减posUp和递增posDown,但在for循环结束时没有重置值。
for(int comeback=1; comeback<=3; comeback++) {
while(posUp.Y > 1 && posDown.Y < 24) {
SetConsoleCursorPosition(screen,posUp);
cout<<" "<<endl;
posUp.Y--;
SetConsoleCursorPosition(screen, posUp);
cout<<"Up"<<endl;
SetConsoleCursorPosition(screen, posDown);
cout<<" ";
posDown.Y++;
SetConsoleCursorPosition(screen, posDown);
cout<<"Down"<<endl;
Sleep(100);
}
SetConsoleCursorPosition(screen, posUp);
cout<<" "<<endl;
SetConsoleCursorPosition(screen, posDown);
cout<< " "<<endl;
posUp = {5,25};
posDown = {68,1};
Sleep(100);
}