加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

切换语句c帮助

发布时间:2020-12-16 10:46:01 所属栏目:百科 来源:网络整理
导读:#include iostreamusing namespace std;int main(){ int score;char grade;cout "Enter your score:" endl;cin score;if (score = 90) grade = 'a';if (score = 80) grade = 'b';if (score = 70) grade = 'c';if (score = 60) grade = 'd';else grade = 'f';
#include <iostream>

using namespace std;

int main()
{
    int score;
char grade;
cout << "Enter your score:" << endl;
cin >> score;
if (score >= 90)
    grade = 'a';
if (score >= 80)
    grade = 'b';
if (score >= 70)
    grade = 'c';
if (score >= 60)
    grade = 'd';
else 
    grade = 'f';
cout << grade << endl;
switch (grade) {
    case 'a':
        cout << "Good job" << endl;
        break;
    case 'c':
        cout << "Fair job" << endl;
        break;
    case 'f':
        cout << "Failure" << endl;
        break;
    default:
        cout << "invalid" << endl;
}
cin.get();
return 0;
  }

当我输入95时,为什么它会给我我的默认开关案例,当我应该得到案例a

解决方法

你错过了一大堆,或者以错误的顺序进行比较.

95大于90,但它也大于80,70和60.所以你会得到’d’.

(而且你没有在你的开关中处理’d’.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读