这个C++程序哪里出错了??

来源:百度知道 编辑:UC知道 时间:2024/05/16 02:55:45
#include<iostream>
using namespace std;
void main(){
int x,y;
cin>>x;
if(x<1)
y=x;
cout<<"y="<<y<<endl;
else if(x>=1&&x<10)
y=2*x-1;
cout<<"y="<<y<<endl;
else
y=3*x-11;
cout<<"y="<<y<<endl;
}

其实你写的没有什么大的错误,只是if...else...语句后面要执行的语句要用{}括起来。
例如:你写的
if(x<1)
y=x;
cout<<"y="<<y<<endl;
要改成:
if(x<1){
y=x;
cout<<"y="<<y<<endl;
}

改好了 没错了

用if 或者 else if 如果里面超过1行 就要用{}
#include<iostream>
using namespace std;
void main(){
int x,y;
cin>>x;
if(x<1)
{
y=x;
cout<<"y="<<y<<endl;
}
else if(x>=1&&x<10)
{
y=2*x-1;
cout<<"y="<<y<<endl;
}
else
{
y=3*x-11;
cout<<"y="<<y<<endl;
}
}

哈~!~认真查一下就行啦~!~