【问题标题】:Is there a way not to print out the else statement when inputting a valid number?有没有办法在输入有效数字时不打印出else语句?
【发布时间】:2022-12-05 06:55:47
【问题描述】:

这是我需要用 C++ 编写的问题 The question/problem

我似乎不知道我的代码的问题。

这是代码:

#include <iostream>

using namespace std;

int main(void)
{
    cout << "Total Purchase Cost: Php ";
    double total;
    cin >> total;
    
    cout << "Loyalty Card Type: ";
    int cardType;
    cin >> cardType;
    double discount = 0;
    double type1 = 0.10;
    double type2 = 0;
    double type3 = 0.15;
    if (cardType == 1)
        discount = total * type1;
    if (cardType == 2)
        discount = total * type2;
    if (cardType == 3)
        discount = total * type3;
    else 
    cout << "Invalid Card";
    cout << "Discounted Cost: Php \n" << discount;
    
    
    return 0;
}

当我尝试运行它时,折扣价是正确的,计算是正确的,输出文本“Discounted Cost: Php ---”似乎工作正常。但是当我开始添加 else 语句时,当我在 if 语句中输入无效数字时,它会输出“Invalid Card”消息。但是当我输入一个有效数字时,else 语句仍然打印出来,即使它不应该打印出来。

【问题讨论】:

  • 使用范围,卢克,使用范围。
  • 您是否意识到您的else 仅附加到最后一个if?你是否也意识到 if 语句都是独立运行的?您想使用 if/else if/else 或开关。投票关闭拼写错误。
  • 如果您将所有图片和图像上传到 Facebook 或 Twitter,效果会更好,而这些网站正是为此而设计的。在 Stackoverflow 上,如果所有相关信息都包含在问题中,它会工作得更好作为纯文本, 这样任何人都可以剪切/粘贴显示的代码并自己尝试。您能否从问题中删除所有图像和照片,并包含与纯文本相同的信息?

标签: c++ if-statement switch-statement


【解决方案1】:

为了解决您眼前的问题,我建议您将除第一个之外的所有if语句替换为else if

if (cardType == 1)
    discount = total * type1;
else if (cardType == 2)
    discount = total * type2;
else if (cardType == 3)
    discount = total * type3;
else
    cout << "Invalid Card" << '
';
cout << "Discounted Cost: Php 
" << discount << '
';

但是,上面的逻辑仍然存在问题,即行

cout << "Discounted Cost: Php 
" << discount;

即使卡无效也会打印,这可能是您不希望看到的。出于这个原因,添加一个变量 bool valid 并且如果该变量是 true 则只打印 "Discounted Cost" 可能会更好:

bool valid = false;

if (cardType == 1)
{
    discount = total * type1;
    valid = true;
}
else if (cardType == 2)
{
    discount = total * type2;
    valid = true;
}
else if (cardType == 3)
{
    discount = total * type3;
    valid = true;
}
else
{
    cout << "Invalid Card" << '
';
}

if ( valid )
{
    cout << "Discounted Cost: Php 
" << discount << '
';
}

除了使用 if...else if 语句链,您还可以使用 switch 语句:

bool valid = false;

switch ( cardType )
{
    case 1:
        discount = total * type1;
        valid = true;
        break;
    case 2:
        discount = total * type2;
        valid = true;
        break;
    case 3:
        discount = total * type3;
        valid = true;
        break;
    default:
        cout << "Invalid Card" << '
';
}

if ( valid )
{
    cout << "Discounted Cost: Php 
" << discount << '
';
}

这个逻辑可以通过使用一个指针来简化一点,可以将其设置为nullptr以指示该卡无效。

double *type = nullptr;

switch ( cardType )
{
    case 1:
        type = &type1;
        break;
    case 2:
        type = &type2;
        break;
    case 3:
        type = &type3;
        break;
    default:
        cout << "Invalid Card" << '
';
}

if ( type != nullptr )
    cout << "Discounted Cost: Php 
" << total * *type << '
';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2022-07-06
    • 1970-01-01
    相关资源
    最近更新 更多