对条件变量使用Char

Using Char for Conditional Variables

本文关键字:Char 变量 条件      更新时间:2023-10-16

我需要帮助添加到一个对年龄进行分类的程序中,以确定个人是否有资格获得人寿保险。规则是添加到以前的程序中,允许用户输入年龄,然后输出他们是未成年儿童、未成年青少年、成年人还是老年人。需要使用字符yes或no来决定是否符合条件。这个规则的增强也将决定资格。以下是迄今为止的规则和代码。

符合年龄17岁或以下18-90岁大于90'n'&lt------新的增强规则代码:

//12 or under         minor child
//13 - 17             minor teenager
//18 - 64             adult
//65 or over      senior adult

#include <iostream>
using namespace std;
int main()
{
    const int MAX_CHILD = 12, MAX_TEEN = 17, MAX_ADULT = 64; MAX_ADULT_ALLOWED = 90;
    int age;

    cout << "How old are you?";
    cin >> age;
    // additional code goes here
    if (age <= MAX_CHILD)
        cout << "You're a minor child.n";
    else if (age <= MAX_TEEN)
        cout << "You're a minor teenager.n";
    else if (age <= MAX_ADULT)
        cout << "You're an adult.n";
    else if
        cout << "You're a senior adult.n";
    else (age <= MAX_ADULT_ALLOWED)

    //if (eligible == "y")
    //cout << "You are eligible for life insurance package.n";
    return 0;
}

我希望这能回答你的问题:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
//12 or under         minor child
//13 - 17             minor teenager
//18 - 64             adult
//65 or over      senior adult
int main()
{
    const int MAX_CHILD = 12, MAX_TEEN = 17, MAX_ADULT = 64, MAX_ADULT_ALLOWED = 90;
    int age;
    char eligible ;
    cout << "How old are you?";
    cin >> age;
    // additional code goes here
    if (age <= MAX_CHILD)
        {
            cout << "You're a minor child.n";
            eligible = 'n';
        }
    else if (age <= MAX_TEEN)
        {
            cout << "You're a minor teenager.n";
            eligible = 'n';
        }
    else if (age <= MAX_ADULT)
        {
            cout << "You're an adult.n";
            eligible = 'y';
        }
    else if (age <= MAX_ADULT_ALLOWED)
        {
            cout << "You're a senior adult.n";
            eligible = 'y';
        }
    else
        {
            cout << "You're a senior adult.n";
            eligible = 'n';
        }


    if (eligible == 'y')
        cout << "You are eligible for life insurance package.n";
    else
         cout << "You are not eligible for life insurance package.n";
    std::system("PAUSE");
    return 0;
}

您可以将打印语句cout << "You are eligible for life insurance package.n";放入满足条件的if中,不需要额外的变量即可获得资格。

if (age <= MAX_CHILD){
    cout << "You're a minor child.n";
    cout << "You are not eligible for life insurance package.n";
    //eligible = 'n';
}
else if (age <= MAX_TEEN){
    cout << "You're a minor teenager.n";
    cout << "You are not eligible for life insurance package.n";
    //eligible = 'n';
}
else if (age <= MAX_ADULT){
    cout << "You're an adult.n";
    cout << "You are eligible for life insurance package.n"; 
    //eligible = 'y';
}
else if(age <= MAX_ADULT_ALLOWED){
    cout << "You're a senior adult.n";
    cout << "You are eligible for life insurance package.n";
    //eligible = 'y';
}
else {
    cout << "You are not eligible for life insurance package.n";
    //eligible = 'n';
}

或者,如果您想使用一个单独的变量来获得资格,那么只需在满足条件的块内为其分配值y,否则将其分配n(我在代码中对这些行进行了注释),最后检查资格。