Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 需要此if语句的帮助吗_C++ - Fatal编程技术网

C++ 需要此if语句的帮助吗

C++ 需要此if语句的帮助吗,c++,C++,首先,让我向您展示代码: // calculate grand total if(unitsPurchased >= 10,19) { discount = .20; totalCost = totalCost - (totalCost * discount); cout << fixed << setprecision(2); cout << "\tBecuase you purchased " &l

首先,让我向您展示代码:

// calculate grand total
if(unitsPurchased >= 10,19)
{
    discount = .20;
    totalCost = totalCost - (totalCost * discount);
    cout << fixed << setprecision(2);
    cout << "\tBecuase you purchased " << unitsPurchased << " Units,\n\tYou get a 20% discount!" << "The grand total is: $" << totalCost << endl;
}

如果一个人买10到19件商品,我想打20%的折扣。我如何修改if语句以反映这一点?我试过使用AND&&但是没有用。如何设置两个数字之间的范围?

您可以这样做:

if(unitsPurchased > 10 && unitsPurchased < 19){
    discount = 0.2; 
}

此if语句检查unitsPurchased是否大于10,是否小于19,这正是您想要的。

您可以这样做:

if(unitsPurchased > 10 && unitsPurchased < 19){
    discount = 0.2; 
}

此if语句检查unitsPurchased是否大于10,是否小于19,这正是您想要的。

您要查找的语句是以下语句之一:

if (unitsPurchased >  10 && unitsPurchased <  19) { // exclude 10, 19
if (unitsPurchased >= 10 && unitsPurchased <  19) { // include 10, exclude 19
if (unitsPurchased >= 10 && unitsPurchased <= 19) { // include 10, 19
像valA,valB这样的表达式实际上使用了逗号运算符,它将同时计算valA和valB,但结果是单个值valB

在您的特定情况下,由于逗号运算符的优先级相对较低,因此更为复杂,因此unitsPurchased>=10,19表示unitsPurchased>=10,19,这相当于:

评估单位被追踪>=10; 评价19例; 使用值19。
因为19不是零,所以它被认为是真的。因此,您的企业更有可能破产,因为每次购买都会带来20%的折扣:-

您要寻找的声明是:

if (unitsPurchased >  10 && unitsPurchased <  19) { // exclude 10, 19
if (unitsPurchased >= 10 && unitsPurchased <  19) { // include 10, exclude 19
if (unitsPurchased >= 10 && unitsPurchased <= 19) { // include 10, 19
像valA,valB这样的表达式实际上使用了逗号运算符,它将同时计算valA和valB,但结果是单个值valB

在您的特定情况下,由于逗号运算符的优先级相对较低,因此更为复杂,因此unitsPurchased>=10,19表示unitsPurchased>=10,19,这相当于:

评估单位被追踪>=10; 评价19例; 使用值19。
因为19不是零,所以它被认为是真的。因此,您的企业更有可能破产,因为每次购买都会带来20%的折扣:-

您必须使用和运营商:

if(unitsPurchased >= 10 && unitsPurchased <= 19)
{
    discount = .20;
    totalCost = totalCost - (totalCost * discount);
    cout << fixed << setprecision(2);
    cout << "\tBecuase you purchased " << unitsPurchased << " Units,\n\tYou get a 20% discount!" << "The grand total is: $" << totalCost << endl;
}
您必须使用&&和运算符:

if(unitsPurchased >= 10 && unitsPurchased <= 19)
{
    discount = .20;
    totalCost = totalCost - (totalCost * discount);
    cout << fixed << setprecision(2);
    cout << "\tBecuase you purchased " << unitsPurchased << " Units,\n\tYou get a 20% discount!" << "The grand total is: $" << totalCost << endl;
}

其他答案都是正确的,如果有人遇到这个问题,我想提醒一下


不要写10其他答案都是正确的,如果有人遇到这个问题,我想提醒一下

不要写10 这样做


这样做。

如果unitsPurchased>=10&&unitsPurchased,我很好奇。如果我买20件商品,我会打折吗?如果unitsPurchased>=10&&unitsPurchased,我很好奇。如果我买了20件商品,我能得到折扣吗?你的生意更可能破产,哈哈哈,也许这就是计划:P你的生意更可能破产,哈哈哈,也许这就是计划:P