C++ 如何使其成为无限循环?
我试图让这个代码从左到右“移动”。我成功地将它从右移到右。现在的问题是,我如何无限地运行它?好吧,我不熟悉C++,因为我是初学者。我试图在互联网上搜索如何使代码无限运行,我发现了一个C++ 如何使其成为无限循环?,c++,codeblocks,C++,Codeblocks,我试图让这个代码从左到右“移动”。我成功地将它从右移到右。现在的问题是,我如何无限地运行它?好吧,我不熟悉C++,因为我是初学者。我试图在互联网上搜索如何使代码无限运行,我发现了一个for(;;)循环,我把它放在我的第一个for循环开始的地方。但它不起作用。你能给我一些提示吗 #include <iostream> #include <cstdlib> #include <string> #include <ctime> #include
for(;;)
循环,我把它放在我的第一个for循环开始的地方。但它不起作用。你能给我一些提示吗
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <iomanip>
#include <windows.h>
using namespace std;
int main ()
{
string a;
cout <<"Enter String : ";
cin >> a;
cout << '\n' << '\n' << '\n' << '\n';
for(int x = 0; x <= 20; x++ ) {
Sleep(200);
system("cls");
cout <<"Enter String : ";
cout << a;
cout << '\n' << '\n' << '\n' << '\n';
cout << setw(x)<< a;
}
for(int y = 20; y <= 20; y-- ) {
Sleep(200);
system("cls");
cout <<"Enter String : ";
cout << a;
cout << '\n' << '\n' << '\n' << '\n';
cout << setw(y)<<a;
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串a;
库塔;
cout通常,您可以编写如下的无限循环:
while (1){
//do coding
}
While循环在()中的语句为true时运行。1始终为true。
您可以随时使用“break;”来停止无限循环。主要的罪魁祸首是这一行for(int y=20;y=0;y--)
然后将这两个for循环都放在中,而(1){}
以下是完整的代码:
int main ()
{
string a;
cout <<"Enter String : ";
cin >> a;
cout << '\n' << '\n' << '\n' << '\n';
while(1){ //infinite loop starts
for(int x = 0; x <= 20; x++ ){
Sleep(200);
system("cls");
cout <<"Enter String : ";
cout << a;
cout << '\n' << '\n' << '\n' << '\n';
cout << setw(x)<< a;
}
for(int y = 20; y >= 0; y-- ){ //make sure this condition
Sleep(200);
system("cls");
cout <<"Enter String : ";
cout << a;
cout << '\n' << '\n' << '\n' << '\n';
cout << setw(y)<<a;
}
}
return 0;
}
int main()
{
字符串a;
库塔;
coutwhile(1)
更像C风格。我们应该更喜欢C++中的while(true)
这样的显式布尔值。我真的不认为为1或true而烦恼是值得的:关于while(“猫王活着”)
?应该是for(;)
@g.tsh这句话让我很惊讶:你到底在问什么?你的问题还不清楚!”我还看到了一个“无效”代码,它的作用是什么?它与我的代码有关吗?“这里每个问题也请问一个问题。
int main ()
{
string a;
cout <<"Enter String : ";
cin >> a;
cout << '\n' << '\n' << '\n' << '\n';
while(1){ //infinite loop starts
for(int x = 0; x <= 20; x++ ){
Sleep(200);
system("cls");
cout <<"Enter String : ";
cout << a;
cout << '\n' << '\n' << '\n' << '\n';
cout << setw(x)<< a;
}
for(int y = 20; y >= 0; y-- ){ //make sure this condition
Sleep(200);
system("cls");
cout <<"Enter String : ";
cout << a;
cout << '\n' << '\n' << '\n' << '\n';
cout << setw(y)<<a;
}
}
return 0;
}