设a为整型变量且其值大于零,请将以下if-else语句改写成switch语句,并完成整个程序的编写。 if ( a<60) m=1; else if(a<70) m=2; else if(a<80) m=3; else if(a<90) m=4; else m=5;
时间: 2024-04-20 18:25:46 浏览: 69
Java语言switch语句的编写
int m;
if (a < 60)
m = 1;
else if (a < 70)
m = ;
else if (a < 80)
m = 3;
else if (a < 90)
m = 4;
else
m = 5;
可以改写为:
int m;
switch(a/10) {
case 6:
m = 1;
break;
case 7:
m = 2;
break;
case 8:
m = 3;
break;
case 9:
m = 4;
break;
default:
m = 5;
break;
}
完整的程序代码如下:
#include <iostream>
using namespace std;
int main() {
int a;
int m;
cout << "请输入一个大于零的整数:";
cin >> a;
switch(a/10) {
case 6:
m = 1;
break;
case 7:
m = 2;
break;
case 8:
m = 3;
break;
case 9:
m = 4;
break;
default:
m = 5;
break;
}
cout << "m的值为:" << m << endl;
return 0;
}
阅读全文