#include <iostream> using namespace std; /********* Begin *********/ //在此处实现一个汽车类 class Car {private: bool door=true; bool light=true; int speed=0; public: void opendoor(); void closedoor(); void openlight(); void closelight(); void upspeed(); void downspeed(); void print(); }; void Car::opendoor() { door=true; } void Car::closedoor() { door=false; } void Car::openlight() { light=true; } void Car::closelight() { light=false; } void Car::upspeed() { speed+=10; } void Car::downspeed() { speed-=10; } void Car::print() {if(door==true) cout<<"车门"<<" "<<"OFF"<<endl; else cout<<"车门"<<" "<<"ON"<<endl; if(light==true) cout<<"车灯"<<" "<<"OFF"<<end; else cout<<"车灯"<<" "<<"NO"<<endl; cout<<"速度 "<<speed<<endl;} /********* End *********/ int main() { /********* Begin *********/ //在此处解析执行输出汽车的整体状态 char cmds[25]; cin>>cmds; int i=0; Car car; while(cmds[i]) { switch(cmds[i]) { case 1:car.opendoor(); break; case 2:car.closedoor(); break; case 3:car.openlight(); break; case 4:car.closelight(); break; case 5:car.upspeed(); break; case 6:car.downspeed(); break; } i++; } car.print(); /********* End *********/ }有什么语法错误
时间: 2023-08-07 13:04:42 浏览: 168
二维vector源码实现
这段代码的语法错误是,在输出车灯状态时,第一个 cout 的字符串中有一个拼写错误,应该将 end 改为 endl。另外,在 switch 语句中,case 后面的数字应该使用单引号将其括起来,表示一个字符,而不是整数。因此代码应该改为:
```
#include <iostream>
using namespace std;
/********* Begin *********/
//在此处实现一个汽车类
class Car {
private:
bool door=true;
bool light=true;
int speed=0;
public:
void opendoor();
void closedoor();
void openlight();
void closelight();
void upspeed();
void downspeed();
void print();
};
void Car::opendoor() {
door=true;
}
void Car::closedoor() {
door=false;
}
void Car::openlight() {
light=true;
}
void Car::closelight() {
light=false;
}
void Car::upspeed() {
speed+=10;
}
void Car::downspeed() {
speed-=10;
}
void Car::print() {
if(door==true) cout<<"车门"<<" "<<"OFF"<<endl;
else cout<<"车门"<<" "<<"ON"<<endl;
if(light==true) cout<<"车灯"<<" "<<"OFF"<<endl;
else cout<<"车灯"<<" "<<"NO"<<endl;
cout<<"速度 "<<speed<<endl;
}
/********* End *********/
int main() {
/********* Begin *********/
//在此处解析执行输出汽车的整体状态
char cmds[25];
cin>>cmds;
int i=0;
Car car;
while(cmds[i])
{
switch(cmds[i])
{
case '1':car.opendoor();
break;
case '2':car.closedoor();
break;
case '3':car.openlight();
break;
case '4':car.closelight();
break;
case '5':car.upspeed();
break;
case '6':car.downspeed();
break;
}
i++;
}
car.print();
/********* End *********/
return 0;
}
```
阅读全文