gpt4 book ai didi

c++ - 如何在 if-else 语句中声明变量?

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:46 24 4
gpt4 key购买 nike

我还是新手,很多东西都不懂唯一缺少的是 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..

最佳答案

只要把它放在if

之外
int x = d / t;
if (x && x >= s)

或者你可能想要这个

int x = d / t;
if (x >= s)

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com