运用简单的if语句判断就行
#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"请输入一个数x,经过判断x>0将输出y为1,x<0将输出y为-1,x=0将y为输出0"<<endl;
cout<<"x=";
cin>>x;
if(x>0)y=1;
else if(x==0)y=0;
else y=-1; //只剩下x<0的情况了
cout<<"y="<<y<<endl;
system("pause");
return 0;
}
本文介绍了一个C++程序,通过if-else语句实现根据用户输入的数x判断其正负,并输出相应的y值:1(x>0)、0(x=0)、-1(x<0)。
613

被折叠的 条评论
为什么被折叠?



