我正在从事 Codecademy 项目,但无法真正找到我的问题的解决方案。
给定 weight
和 planet-number
作为输入,本练习的目标是使用 switch 语句返回您需要根据 converted-weight
到达的 planet-number
你想去。
计算 converted-weight
后,我想根据 planet-number
是否在有效范围内打印一些条件消息。我在比较 planet-number
的有效范围时遇到困难(例如,在这种情况下为 0-6
)。这是我尝试过的代码:
#include <iostream>
int main() {
double weight;
int planet;
double planetw;
std::cout << "What is your Earth weight?\n";
std::cin >> weight;
std::cout << "What Planet number would you like to fight on? \n";
std::cin >> planet;
switch (planet) {
case 1:
planetw = weight * 0.38;
break;
case 2:
planetw = weight * 0.91;
break;
case 3:
planetw = weight * 0.38;
break;
case 4:
planetw = weight * 2.34;
break;
case 5:
planetw = weight * 1.06;
break;
case 6:
planetw = weight * 0.92;
break;
case 7:
planetw = weight * 1.19;
break;
default:
std::cout << "That is not a planet option\n";
break;
}
if (planet) != range (0,6) {
std::cout << "Pick a valid planet number next time.\n";
} else {
std::cout << "Your traget weight to fight there is " << weight << " .\n";
}
}
答案 0 :(得分:2)
由于您已经在 switch
情况下处理了所需的值范围,因此您可以使用已经存在的 default
情况,例如:
#include <iostream>
int main() {
double weight;
int planet;
double planetw;
std::cout << "What is your Earth weight?\n";
std::cin >> weight;
std::cout << "What Planet number would you like to fight on? \n";
std::cin >> planet;
switch (planet) {
case 1:
planetw = weight * 0.38;
break;
case 2:
planetw = weight * 0.91;
break;
case 3:
planetw = weight * 0.38;
break;
case 4:
planetw = weight * 2.34;
break;
case 5:
planetw = weight * 1.06;
break;
case 6:
planetw = weight * 0.92;
break;
case 7:
planetw = weight * 1.19;
break;
default:
std::cout << "That is not a planet option\n";
std::cout << "Pick a valid planet number next time.\n";
return 0;
}
std::cout << "Your target weight to fight there is " << planetw << " .\n";
return 0;
}
但是,要回答您的问题,您可以进行如下范围检查:
#include <iostream>
int main() {
double weight;
int planet;
double planetw;
std::cout << "What is your Earth weight?\n";
std::cin >> weight;
std::cout << "What Planet number would you like to fight on? \n";
std::cin >> planet;
switch (planet) {
case 1:
planetw = weight * 0.38;
break;
case 2:
planetw = weight * 0.91;
break;
case 3:
planetw = weight * 0.38;
break;
case 4:
planetw = weight * 2.34;
break;
case 5:
planetw = weight * 1.06;
break;
case 6:
planetw = weight * 0.92;
break;
case 7:
planetw = weight * 1.19;
break;
default:
std::cout << "That is not a planet option\n";
break;
}
if (planet < 1 || planet > 7) {
std::cout << "Pick a valid planet number next time.\n";
} else {
std::cout << "Your target weight to fight there is " << planetw << " .\n";
}
return 0;
}
如果你真的想使用像 if (planet != range(....))
这样的实际语法,那当然是可行的,但需要更多的设置工作。你可以这样做:
#include <iostream>
struct sRange
{
int minValue;
int maxValue;
bool operator==(int value) const {
return (value >= minValue && value <= maxValue);
}
bool operator!=(int value) const {
return (value < minValue || value > maxValue);
}
};
bool operator==(int value, const sRange &rhs) {
return rhs == value;
}
bool operator!=(int value, const sRange &rhs) {
return rhs != value;
}
sRange range(int minValue, int maxValue) {
return sRange{minValue, maxValue};
}
int main() {
double weight;
int planet;
double planetw;
std::cout << "What is your Earth weight?\n";
std::cin >> weight;
std::cout << "What Planet number would you like to fight on? \n";
std::cin >> planet;
switch (planet) {
case 1:
planetw = weight * 0.38;
break;
case 2:
planetw = weight * 0.91;
break;
case 3:
planetw = weight * 0.38;
break;
case 4:
planetw = weight * 2.34;
break;
case 5:
planetw = weight * 1.06;
break;
case 6:
planetw = weight * 0.92;
break;
case 7:
planetw = weight * 1.19;
break;
default:
std::cout << "That is not a planet option\n";
break;
}
if (planet != range(1, 7)) { // or: if (range(1, 7) != planet)
std::cout << "Pick a valid planet number next time.\n";
} else {
std::cout << "Your target weight to fight there is " << planetw << " .\n";
}
return 0;
}
答案 1 :(得分:1)
因为您在 if
语句中询问范围,所以我不使用 switch
语句回答您的问题。
你可以这样做
if (planet > 7 || planet < 1) {
std::cout << "That is not a planet option\n";
std::cout << "Pick a valid planet number next time.\n";
}
else {
double ratio[7] = {0.38, 0.91, 0.38, 2.34, 1.06, 0.92, 1.19};
planetw = weight * ratio[planet-1];
std::cout << "Your target weight to fight there is " << planetw << " .\n";
}
答案 2 :(得分:0)
你可以简单地把它写成两个条件来实现你的范围逻辑:
if (planet > 7 || planet < 1) {
}
else {}
注意:你从索引 1 开始,所以你的范围是 1 到 7
这些指令有时会在汇编代码 jg(如果更大则跳转)和 jl(如果更低则跳转)中进行翻译,但是编译器使用的一种适用于无符号整数的技巧是减去低端,然后检查它是否小于或等于到最大界限。
int tmp = planet - 1;
if (tmp > 6) { ... must be a valid planet message... }