繁体   English   中英

做加法时如何使负数不计算在内(C++)

[英]How to make negative numbers not count when doing addition (C++)

我尝试为学校作业制作这样的程序,但它不起作用有人可以帮助我吗,我真的不知道该去哪里

#include <iostream>

using namespace std;

int main()
{
   int a;
   int b;
   int c;
   int d;
   cout<<"enter 3 numbers"<<endl;
   cin>>a;
   cin>>b;
   cin>>c;
   d = a + b + c
   if ( a <= 1 - = 0 )
    cout<<d<<endl;
   if ( b < 1 - = 0 )
   cout<<d<<endl;
   else ( c < 1 - = 0 )



   return 0;

你可能想尝试这样的事情:

int d = 0; // Initial value
if (a > 0)
{
    d = d + a;
}
if (b > 0)
{
    d = d + b;
}
if (c > 0)
{
    d = d + c;
}

在上面的代码中,项仅在它们为正时才添加到d变量中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM