遇到麻烦,没有循环

时间:2015-03-04 02:15:17

标签: c++ loops

所以在我的计算机科学课上,我们做了一个月球着陆器游戏。在代码的最后,我们应该询问用户是否想再次运行游戏,而我已经建立了yes和no循环,它仍然无法工作。它让我输入'Y'和'N',但它只是在单词之后断开。

  // Include Section
  #include <iostream>
  #include <cstdlib>
  #include <iomanip>
  using namespace std;

  // Main Program
  int main( )
  {
   // Variable Declarations
   double E;            // Engine output
   double V;            // Velocity output
   double A;            // Altitude output
   double T;            // Time output
   double F;            // Fuel consumption
   double Th;           // Thrust
   double x;            // The variable output the user puts in
   double G;            // The gravity of the moon
   double Acceleration;// The acceleration found between the force and the    
   gravity
   double M;            // Total Mass of the lander
   double Force;        // The force opposing the lunar gravity
   double W;            // Weight 
   double Thrust;       // The initial thrust value
   char Ans;

   A=2300;
   V=0;
   T =0;
   F=300;
   G=-1.62;
   Thrust=43900;
   M= 6831;


// Output Identification
system("CLS");

cout << "Welcome to the Lunar Lander simulation!" << endl;
cout << endl;
cout << "You are in command of the Lunar Lander on its final approach to the moon's surface."
    " You need to control the thrust to the main decent engine."
    " You will adjust it each second by typing in the percent thurst that you would like for the next second."
    " Your goal is to end land the spacecraft at a velocity of less than 1 meter/second."
    <<endl;
cout << endl;
cout << "GOODLUCK!"<<endl;
cout << endl;
cout << setw(8) << "Sec" << setw(10) << "Alt" << setw(10) << "Speed" << setw(8) << "Fuel" << endl;

while(F > 0)
{
    if(A > 0)
    {
        cout << setw(8) << T << setw(10) << A << setw(10) << V 
            << setw(8) << F << setw(14) << "     Enter percent burn (0-100):";
        cin >> x;
        if( x < 0|x > 100)
        {
            cout << "Please pick a valid percentage."<< endl;
        }
        else
        {
            T++;
            x=x/100;
            Th= x*Thrust;
            E= 13.6*x;
            F= F-E;
            M =M-E;
            W=M*G;
            Force=Th+W;
            Acceleration=Force/M;
            V=V+Acceleration*1;
            A=A+V*1-.5*Acceleration*1;
        }
    }
    else
    { 
        if(V <= -1 & V >=-3)
        {
            cout << "Congratulations!" << endl;
            cout << "Would you like to run the simulation again? <Y/N>"<<endl;
            cin >> Ans;
            if(Ans == 'Y'|| Ans == 'y')
                return 0;
            else
                break;
        }
        else
        {
            cout << "Game Over" <<endl;
            cout<< "Would you like to run the simulation again? <Y/N>"<<endl;
            cin >> Ans;
            if(Ans == 'Y'||Ans == 'y')
                return 0;
            else
                break;
        }
    }
    if(F<=0)
    {
    cout<< "Would you like to run the simulation again? <Y/N>"<<endl;
    cin >> Ans;
        if(Ans == 'Y'|| Ans=='y')
            return 0;
        else
            break;
    }
}

}

3 个答案:

答案 0 :(得分:0)

cout<< "Would you like to run the simulation again? <Y/N>"<<endl;
cin >> Ans;
    if(Ans == 'Y'|| Ans=='y')
        return 0;
    else
        break;

返回0无助于您的代码再次运行循环。 我想你可以用

的形式修改你的代码
while(1){
cout<< "Would you like to run the simulation again? <Y/N>"<<endl;
cin >> Ans;
if(Ans == 'N'||Ans == 'n')break;
}

希望这对你有所帮助。

答案 1 :(得分:0)

稍微优雅的解决方案。

std::string line;
do {
  // code to be repeated upon "Y"
} while (std::getline(std::cin, line) && line == "Y");

答案 2 :(得分:0)

在解决了使用|代替||的明显错误后,其他问题是:

如果您引入了一些简单的bool变量来确定代码中的循环是否应该继续,那将会有所帮助。

例如:

 bool inputOk;
 do
 {
    inputOk = true;
    cout << setw(8) << TimeOutput << setw(10) << Altitude << setw(10) << V
          << setw(8) << F << setw(14) << "     Enter percent burn (0-100):";
    cin >> x;
    if (x < 0 || x > 100)
    {
        cout << "Please pick a valid percentage." << endl;
        inputOk = false;
    }
}  while (!inputOk);

您现在不需要在此代码之后拥有else,因为这只会在输入正常之前离开循环。

鉴于此,您应该使用与上面相同的技术进行整个循环,在那里您要求运行另一个模拟:

bool runLoop;  // instead of while (F > 0)
do 
{
   runLoop = true;
   //.. your other code
   //...
   cout << "Would you like to run the simulation again? <Y/N>" << endl;
   cin >> Ans;
   if (Ans != 'Y' && Ans != 'y')
        runLoop = false;
//...
} while (runloop);

很简单。您只需继续运行循环,直到用户不想再运行模拟。您可以通过编写do-while并测试runLoop状态来实现此目的。如果用户决定退出,则runLoop设置为false,因此循环终止。

相关问题