我还是这个新手,我真的不了解很多事情
唯一缺少的是x = d / t,我不知道该放在哪里...
我已经尝试了各种方法,但仍然不知道在哪里放置它。

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{


     int d, t, s, z;
     cout<<"Enter the distance of your destination:"<<endl;
     cin >>d;
     cout<<"Enter the time you want to get there:"<<endl;
     cin >>t;

     for (z=1; z<=5; z++)
     {
      cout<<"Enter your speed:"<<endl;
      cin>>s;

      if (int x=d/t && x>=s)
      {

              cout<<"Maintain your speed it is the appopriate speed: "<<s<<"mph"<<endl;
              break;
              }
              else
              {

              cout<<"Go faster at this rate you won't get to your destination on time"<<endl;
              }
              }
    system("PAUSE");
    return EXIT_SUCCESS;
}

The Output always shows the if statement even though it should already show the else statement..
I really need help on this..

最佳答案

这是代码:

   int main(int argc, char *argv[])

{

 int d, t, s, z;
 cout<<"Enter the distance of your destination:"<<endl;
 cin >>d;
 cout<<"Enter the time you want to get there:"<<endl;
 cin >>t;

 for (z=1; z<=5; z++)
 {
  cout<<"Enter your speed:"<<endl;
  cin>>s;
 int x=d/t;
  if (x>=s)
  {

          cout<<"Maintain your speed it is the appopriate speed: "<<s<<"mph"<<endl;
          break;
          }
          else
          {

          cout<<"Go faster at this rate you won't get to your destination on     time"<<endl;
          }
          }
system("PAUSE");
return EXIT_SUCCESS;
       }

关于c++ - 如何在if-else语句中声明变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22282542/

10-10 00:58