运行程序时,即使我输入no,它也会添加75.我不知道它为什么要这么做.我对此还是个新手.

Console.WriteLine("Do you smoke? (Y/N)");
            SmokerStatus = Console.ReadLine();
            if (SmokerStatus != "y" || SmokerStatus != "Y" || SmokerStatus != "yes" || SmokerStatus != "Yes")
            {
                BaseQuote = BaseQuote + Convert.ToInt32(75);
            }
            else if (SmokerStatus != "n" || SmokerStatus != "N" || SmokerStatus != "no" || SmokerStatus != "No")
            {
                BaseQuote = BaseQuote - Convert.ToInt32(100);
            }

这是完整的节目,我有同样的问题与条件状态部分.在那两个部分之前,一切都运行得很好.

using System;
using System.Collections;
namespace Insurance
{
    internal class Insurance
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Nationwide Heath Insurance Co.\n");
#pragma warning disable CS0168
#pragma warning disable CS0165
#pragma warning disable CS8600
#pragma warning disable CS8321
#pragma warning disable CS0219
#pragma warning disable CS8601

            int CustomerID = 1;
            int Age;
            decimal BaseQuote = 250;
            String Eir, Gender, FirstName, LastName, UserAge, ConditionStatus, BadConditions, SmokerStatus;
            bool ConditionBool;

            decimal CoverageAmount, BasePremiumRate, AgePremiumFactor,
            SmokingPremiumFactor, HealthConditionFactor, TotalPremium;

            Console.WriteLine("Please enter your first name:");
            FirstName = Console.ReadLine();
            Console.WriteLine("Please enter your last name ");
            LastName = Console.ReadLine();
            Console.WriteLine("Please enter your gender ");
            Gender = Console.ReadLine();
            if (Gender != "male" || Gender != "Male" || Gender != "m" || Gender != "M")
            {
                BaseQuote = BaseQuote + Convert.ToInt32(250);
            }
            else if (Gender != "female" || Gender != "Female" || Gender != "f" || Gender != "F")
            {
                BaseQuote = BaseQuote + Convert.ToInt32(175);
            }
            Console.WriteLine("Please enter your age");
            UserAge = Console.ReadLine();
            Age = Convert.ToInt32(UserAge);
            if (Age < 35)
            {
                BaseQuote = BaseQuote + Convert.ToInt32(50);
            }
            else if (Age > 35 && Age < 55)
            {
                BaseQuote = BaseQuote + Convert.ToInt32(100);
            }
            else if (Age > 55 && Age < 70)
            {
                BaseQuote = BaseQuote + Convert.ToInt32(162.5);
            }
            else if (Age >= 70)
            {
                Console.WriteLine("No Quote Provided");
                Environment.Exit(0);
            }
            Console.WriteLine("Please enter your adress (eircode)");
            Eir = Console.ReadLine();
#pragma warning disable CS8602
            Eir = Eir.ToUpper();
            Console.WriteLine("Do you have an existing health conditions? (Y/N)");
            ConditionStatus = Console.ReadLine();
            if (ConditionStatus != "y" || ConditionStatus != "Y" || ConditionStatus != "yes" || ConditionStatus != "Yes")
            {
                BaseQuote = BaseQuote + Convert.ToInt32(125);
            }
            else if (ConditionStatus != "n" || ConditionStatus != "N" || ConditionStatus != "no" || ConditionStatus != "No")
            {
                BaseQuote = BaseQuote + Convert.ToInt32(0);
            }
            Console.WriteLine("Please list conditions using comas. If none then type 'None' or 'NA'");
            BadConditions = Console.ReadLine();
            Console.WriteLine("Do you smoke? (Y/N)");
            SmokerStatus = Console.ReadLine();
            if (SmokerStatus != "y" || SmokerStatus != "Y" || SmokerStatus != "yes" || SmokerStatus != "Yes")
            {
                BaseQuote = BaseQuote + Convert.ToInt32(75);
            }
            else if (SmokerStatus != "n" || SmokerStatus != "N" || SmokerStatus != "no" || SmokerStatus != "No")
            {
                BaseQuote = BaseQuote - Convert.ToInt32(100);
            }
            /* string BadConditions = "Do you have any existing health conditions? ( y/n )";
            Console.WriteLine(BadConditions);
            ConditionBool = AskCondition(ref BadConditions); */

            Console.WriteLine($"Quote ID: {CustomerID}\n");
            Console.WriteLine($"Name: {FirstName} {LastName}");
            Console.WriteLine($"Age: {Age}");
            Console.WriteLine($"Gender: {Gender}");
            Console.WriteLine($"Address: {Eir}");
            Console.WriteLine($"Health Conditions: {BadConditions}");
            Console.WriteLine($"Quote: {BaseQuote}");
        }

        /*  private static bool AskCondition(ref string BadConditions)
         {
             bool ConditionBool;
             while (true)
             {
                 string ConditionAnswer = Console.ReadLine();
                 if (ConditionAnswer != null && ConditionAnswer == "y")
                 {
                     ConditionBool = true;
                     Console.WriteLine("Please specify your health condition using a coma:");
                     BadConditions = Console.ReadLine();
                     string[] HealthConditions = new string[] { "" };
                     HealthConditions = BadConditions.Split(',');
                     break;
                 }
                 else if (ConditionAnswer != null && ConditionAnswer == "n")
                 {
                     ConditionBool = false;
                     break;
                 }
                 else
                 {
                     Console.WriteLine("Only y or n Allowed");
                 }
             }

             return ConditionBool;
         } */
    }
}

推荐答案

在比较时,您似乎切换了使用的!=(不等于)而不是==(等于)运算符.因此,如果在您的版本中输入no,您将得到75与BaseQuote相加的块(因为no不等于yYyesYes中的任何一个)

Console.WriteLine("Do you smoke? (Y/N)");
SmokerStatus = Console.ReadLine();
if (SmokerStatus == "y" || SmokerStatus == "Y" || SmokerStatus == "yes" || SmokerStatus == "Yes")
{
    BaseQuote = BaseQuote + Convert.ToInt32(75);
}
else if (SmokerStatus == "n" || SmokerStatus == "N" || SmokerStatus == "no" || SmokerStatus == "No")
{
    BaseQuote = BaseQuote - Convert.ToInt32(100);
}

Csharp相关问答推荐

需要更改哪些内容才能修复被覆盖的财产中的无效警告CS 8765?

无法使用ternal- .net修复可空警告

C#类主构造函数中的调试参数

如何使用C#和Graph API从Azure Directory获取用户详细信息

处理. netstandard2.0项目中HttpClient.SslProtocol的PlatformNotSupportedException问题""

从ASP.NET Core中的枚举字段填充 Select 选项时,将默认的第一个选项添加为 Select 元素

如何在实体框架中添加包含列表?

如何使用EF Core和.NET 8来upsert到具有多对多关系的表?

我如何让我的秒表保持运行场景而不重置

在C#中反序列化/序列化具有混合元素顺序的XML时出现问题

解决方案:延长ABP框架和ANGING OpenIddict中的令牌生命周期

用于ASP.NET核心的最小扩展坞

如何使用类似于[SELECT*FROM&Q;&Q;WHERE&Q;]SQL查询的System.Data.Entity创建查询?

Azure函数正在返回值列表,但该列表在Chrome中显示为空

如何在单击按钮后多次异步更新标签

如何在.NET Core 8中自定义标识用户模型

并发表更新.EF核心交易

我是否以错误的方式使用了异步延迟初始化?

不寻常的C#语法

C#、Visual Studio代码、调试器、错误处理变量请求.未知错误:0x80131502,