C ++初学者被困

时间:2018-01-13 07:10:53

标签: c++

#include <iostream>
using namespace std;

int main()
{
int score;
  cout << "Enter your score (0-100): ";
  cin >> score;

  if (score >= 90 && score <= 100){
      cout << "You have earned an A" + (score);
  }else if(score >= 80 && score <= 89) {
      cout << "You have earned a B" + (score);
  }else if(score >= 70 && score <= 79) {
      cout << "You have earned a C" + (score);
  }else if(score >= 60 && score <= 69) {
      cout << "You have earned a D" + (score);
  }else if(score >= 0 && score <= 59) {
      cout << "You have earned a F" + (score);
  }


  return 0;
}

当我运行程序时,输出不起作用。说明是写一个程序,要求用户输入在某些考试中获得的分数(0-100)(为简单起见,请假设只允许整数值)并打印对应于分数的字母等级,假设下面显示的比例:

90-100→A

80-89→B

70-79→C

60-69→D

低于60→F

1 个答案:

答案 0 :(得分:3)

我猜你是一名java开发人员, 要打印字符串和变量值,我们在java中使用

System.out.print("Value of a is:" + a);

我们使用&#39; +&#39;用于字符串连接的运算符。 但在C ++中我们可以写为,

cout<<"Value of a is:"<<a;

因此,在您的代码中替换所有&#39; +&#39;与&#39;&lt;&lt;&#; (插入操作符)在打印语句中。

如需更多参考,请访问:cout reference