我有以下任务:
一个程序需要四(4)个输入,然后使用条件语句执行以下操作:
我已经成功地解决了所有这些问题,因为它们都很容易,但是我想问几件事情,同时从你那里得到反馈。
还要记住,使用循环,数组,函数.是不允许的,所以如果我们只讨论条件的话,我会很高兴的。
#include "stdafx.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
float val1, val2, val3, val4;
float sum = 0, multiply = 1;
cout << "Enter the first number: ";
cin >> val1;
if(val1 < 20) {
multiply *= val1;
} else if(val1 > 25) {
sum += val1;
}
cout << "Enter the second number: ";
cin >> val2;
if(val2 < 20) {
multiply *= val2;
} else if(val2 > 25) {
sum += val2;
}
cout << "Enter the third number: ";
cin >> val3;
if(val3 < 20) {
multiply *= val3;
} else if(val1 > 25) {
sum += val3;
}
cout << "Enter the fourth number: ";
cin >> val4;
if(val4 < 20) {
multiply *= val4;
} else if(val4 > 25) {
sum += val4;
}
if(val1 <= 25) cout << "The number(s) below or equal to 25 are/is: " << val1 << endl;
if(val2 <= 25) cout << "The number(s) below or equal to 25 are/is: " << val2 << endl;
if(val3 <= 25) cout << "The number(s) below or equal to 25 are/is: " << val3 << endl;
if(val4 <= 25) cout << "The number(s) below or equal to 25 are/is: " << val4 << endl;
cout << "When we multiply the numbers below 20 we get: " << multiply << endl;
cout << "When we add the numbers above 25 we get: " << sum << endl;
return 0;
}发布于 2016-12-14 22:59:43
关于变量数:
为了显示所有“小”值,可以引入一个string类型的变量,在该变量中保存所有小数字。但是,由于您还没有了解函数和数组,这将使您的代码比现在复杂得多。
所以现在,只需保持程序的原样,每个数字使用一个变量。
在剩下的评论中,我将直接从顶部到代码的底部。
#include "stdafx.h"
#include "iostream"#include "stdafx.h"很好。当包含文件来自您自己的项目时( stdafx.h会这样做),您应该用双引号包围它。
但是所有其他包含的文件(特别是像iostream这样的系统头)不应该包含在"double quotes"中,而应该放在<angle brackets>中。所以应该是#include <iostream>。
using namespace std;一旦开始编写严肃的代码,就应该省略using namespace std。这一行将从std命名空间中导入数百个名称,例如min、max、sort,这取决于您所包含的系统标头。这可能会导致在键入错误或忘记声明也在该命名空间中定义的函数时造成混乱。
int _tmain(int argc, _TCHAR* argv[])
{
float val1, val2, val3, val4;
float sum = 0, multiply = 1;尽可能使用数据类型double而不是float,因为它更精确(15位而不是7位)。
cout << "Enter the first number: ";
cin >> val1;想象一下,当程序的用户在这里输入一个数字而只是输入hello program. how are you today?时会发生什么。在这种情况下,val1将不会获得任何值,程序将使用之前存储在那里的任何值。这会导致不可预测的行为。
要解决这个问题,请编写以下代码:
if (!(std::cin >> val1)) {
std::cerr << "Error: could not read the first number\n";
return 1; // Any nonzero value means failure.
}继续您的代码…
if(val1 < 20) {
multiply *= val1;
} else if(val1 > 25) {
sum += val1;
}非常好。简短可读的。我只需将变量名multiply更改为product,因为变量通常会获得被动名称。稍后,当您编写函数时,这些函数将得到活动名称,如multiply两个数字。
cout << "Enter the second number: ";
cin >> val2;
if(val2 < 20) {
multiply *= val2;
} else if(val2 > 25) {
sum += val2;
}
cout << "Enter the third number: ";
cin >> val3;
if(val3 < 20) {
multiply *= val3;
} else if(val1 > 25) {
sum += val3;
}
cout << "Enter the fourth number: ";
cin >> val4;
if(val4 < 20) {
multiply *= val4;
} else if(val4 > 25) {
sum += val4;
}
if(val1 <= 25) cout << "The number(s) below or equal to 25 are/is: " << val1 << endl;
if(val2 <= 25) cout << "The number(s) below or equal to 25 are/is: " << val2 << endl;
if(val3 <= 25) cout << "The number(s) below or equal to 25 are/is: " << val3 << endl;
if(val4 <= 25) cout << "The number(s) below or equal to 25 are/is: " << val4 << endl;are/is看上去并不令人信服。为了使输出更好,您可以计算小于或等于25的数字。然后,添加以下代码:
if (lessOrEqual25Count == 0) {
std::cout << "No numbers are below or equal to 25.\n";
} else {
if (lessOrEqual25Count == 1) {
std::cout << "One number is below or equal to 25:";
} else {
std::cout << lessOrEqual25Count << " numbers are below or equal to 25:";
}
if (val1 <= 25) std::cout << " " << val1;
if (val2 <= 25) std::cout << " " << val2;
if (val3 <= 25) std::cout << " " << val3;
if (val4 <= 25) std::cout << " " << val4;
std::cout << "\n";
}继续您的代码…
cout << "When we multiply the numbers below 20 we get: " << multiply << endl;
cout << "When we add the numbers above 25 we get: " << sum << endl;
return 0;
}总之,这是个不错的节目。它工作得很好,而且您很快就会了解到数组和函数。它们将使编程变得更容易,因为目前您必须一遍又一遍地重复大量代码。
https://codereview.stackexchange.com/questions/149913
复制相似问题