时间:2019-05-11 标签:c++intswitch
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22028314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ int switch statement
提问by Jose Meza
i have been working on a switch for integers for the last hour and a half now, i know how to do switch with char but this seems much hard for me.any advice will be appreciated.the problem i have is that i cant accept grades over 100 which this switch currently does
在过去的一个半小时里,我一直在研究整数转换,我知道如何用字符转换,但这对我来说似乎很难。任何建议将不胜感激。我的问题是我不能接受成绩此开关当前执行的 100 多个
int testScore;
cout <<"Enter your test score and I will tell you \n";
cout <<"the letter grade you earned ";
cin >> testScore;
switch(testScore/10)
{
case 10:
case 9:
cout <<"Your grade is A.\n";
break;
case 8:
cout <<"Your grade is B.\n";
break;
case 7:
cout <<"Your grade is C.\n";
break;
case 6:
cout << "Your grade is D.\n";
break;
case 5:
cout << "Your grade is F.\n";
break;
default:
cout << "That score isn't valid\n";
}
回答by Seyon
You're dividing by 10.0, which is a double and this will not compile. This has to be changed to 10. Also, you should precede the switch statement with an if statement that checks if it's in a valid range.
你除以 10.0,这是一个双倍,这不会编译。这必须更改为 10。此外,您应该在 switch 语句之前使用 if 语句来检查它是否在有效范围内。
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int testScore;
cout <<"Enter your test score and I will tell you \n";
cout <<"the letter grade you earned \n";
cin >> testScore;
if (testScore<=100 && testScore>=0)
switch(testScore/10)
{
case 10:
case 9:
cout <<"Your grade is A.\n";
break;
case 8:
cout <<"Your grade is B.\n";
break;
case 7:
cout <<"Your grade is C.\n";
break;
case 6:
cout << "Your grade is D.\n";
break;
case 5:
cout << "Your grade is F.\n";
break;
default:
cout << "That score isn't valid\n";
}
else
cout <<"That score isn't valid\n";
return 0;
}
回答by Chris
You really should do this with 'if' not a 'switch'. Something like the following code (not tested):
你真的应该用'if'而不是'switch'来做到这一点。类似于以下代码(未测试):
if (testScore >=0 && testScore <= 100)
{
char grade;
if (testScore >= 90)
grade = 'A';
else if (testScore >= 80)
grade = 'B';
else if (testScore >= 70)
grade = 'C';
else if (testScore >= 60)
grade = 'D';
else
grade = 'F';
cout << "Your grade is " << grade << endl;
}
else
{
cout << "Score of " << testScore << " is not valid" << endl;
}
回答by Jose Meza
i just decided to do it this way, thanks for all your answers
我只是决定这样做,感谢您的所有回答
int testScore;
cout <<"Enter your test score and I will tell you \n";
cout <<"the letter grade you earned ";
cin >> testScore;
if (testScore >= 0 && testScore <=100)
{
switch(testScore/10)
{
case 10:
case 9:
cout <<"Your grade is A.\n";
break;
case 8:
cout <<"Your grade is B.\n";
break;
case 7:
cout <<"Your grade is C.\n";
break;
case 6:
cout << "Your grade is D.\n";
break;
default:
cout << "Your grade is F.\n";
}
}
else
cout <<"That score isn't valid" << endl;