根据c#中两个出生日期的星象之间的兼容性

本文关键字:出生日期 兼容性 之间 两个 根据 | 更新日期: 2023-09-27 17:52:13

我有一个关于我的小项目的问题,该项目是基于通过检查两个出生日期来检查两个占星之间的兼容性(例如:第一个人的出生日期(1-19(第一个数字代表月份,第二个数字代表日期)),然后相应地输入第二个人的工作。当我输入两个出生日期时,我的代码工作正常,这意味着它是根据出生日期告诉月份,日期和占星。但是,当我试图找到两个特定的星星之间的兼容性,它只计算第一个结果在if else语句,如果结果在第一个if else控制结构没有找到它不跳转到下一个else if语句,它直接跳转到最后一个else语句…我是第一次在这里张贴问题,请帮助我…如果有人可以这里是我的代码样本!!

DateTime date = Convert.ToDateTime(Console.ReadLine());
            Console.WriteLine("Month:" + date.Month);
            Console.WriteLine("Day:" + date.Day);
            month = date.Month;
            day = date.Day;
            month1=date1.Month;
            day1=date1.Day;
string[] stringArr = { "January", "February", "march", "April", "may", "June", "July", "august", "September", "October", "November", "December"};
switch (month)
        {
            case 1:
                if (day <= 19)
                    return "Capricorn";
                else
                    return "Aquarius";
            case 2:
                if (day <= 18)
                    return "Aquarius";
                else
                    return "Pisces";
            case 3:
                if (day <= 20)
                    return "Pisces";
                else
                    return "Aries";
            case 4:
                if (day <= 19)
                    return "Aries";
                else
                    return "Taurus";
            case 5:
                if (day <= 20)
                    return "Taurus";
                else
                    return "Gemini";
            case 6:
                if (day <= 20)
                    return "Gemini";
                else
                    return "Cancer";
            case 7:
                if (day <= 22)
                    return "Cancer";
                else
                    return "Leo";
            case 8:
                if (day <= 22)
                    return "Leo";
                else
                    return "Virgo";
            case 9:
                if (day <= 22)
                    return "Virgo";
                else
                    return "Libra";
            case 10:
                if (day <= 22)
                    return "Libra";
                else
                    return "Scorpio";
            case 11:
                if (day <= 21)
                    return "Scorpio";
                else
                    return "Sagittarius";
            case 12:
                if (day <= 21)
                    return "Sagittarius";
                else
                    return "Capricorn";
        return "";
       }
另一个人的出生日期同时计算。
if (ob.month.Equals(1) && ob1.month1.Equals(1))
{
    if (ob.day <= 19 && ob1.day1 <= 19)
    {
        Console.WriteLine("Compatibility of Capricon with Capricon");
    }
    // else if (ob.day <= 19 && ob1.day1 > 19)
    else
    {
        Console.WriteLine("Compatibility of Capricon with Aquarius");
    }
}
else if (ob.month.Equals(1) && ob.month1.Equals(2))
{
    if (ob.day <= 19 && ob1.day1 <= 18)
    {
        Console.WriteLine("Compatibility of Capricon with Aquarius");
    }
    //else if (ob.day <= 19 && ob1.day1 > 18)
    else
    {
        Console.WriteLine("Compatibility of Capricon with Pisces");
    }
}
else if (ob.month.Equals(1) && ob.month1.Equals(3))
{
    if (ob.day <= 19 && ob.day1 <= 20)
    {
        Console.WriteLine("Compatibility of Capricon with Pisces");
    }
    else
    {
        Console.WriteLine("Compatibility of Capricon with Aries");
    }
}
else
    {consle.writeLine("invalid");
    }

这段代码只对第一个if else语句给出响应,而不是对其他if else语句。请帮助我,我该如何解决这个问题!!

根据c#中两个出生日期的星象之间的兼容性

你让事情变得很困难。让它更简单。

class Program
{
    enum ZodiacSign
    {
        Aries, // March 21 - April 20
        Taurus, // April 21 - May 21
        Gemini, // May 22 - June 21
        Cancer, // June 22 - July 22
        Leo, // July 23 -August 21
        Virgo, // August 22 - September 23
        Libra, // September 24 - October 23
        Scorpio, // October 24 - November 22
        Sagittarius, // November 23 - December 22
        Capricorn, // December 23 - January 20
        Aquarius, // January 21 - February 19
        Pisces // February 20- March 20
    }
    static ZodiacSign BirthdayToZodiacSign(DateTime birthday)
    {
        var periodEndList = new[] {
            new { PeriodEnd = new DateTime(birthday.Year, 1, 20), ZodiacSign = ZodiacSign.Capricorn },
            new { PeriodEnd = new DateTime(birthday.Year, 2, 19), ZodiacSign = ZodiacSign.Aquarius },
            new { PeriodEnd = new DateTime(birthday.Year, 3, 20), ZodiacSign = ZodiacSign.Pisces },
            new { PeriodEnd = new DateTime(birthday.Year, 4, 20), ZodiacSign = ZodiacSign.Aries },
            new { PeriodEnd = new DateTime(birthday.Year, 5, 21), ZodiacSign = ZodiacSign.Taurus },
            new { PeriodEnd = new DateTime(birthday.Year, 6, 21), ZodiacSign = ZodiacSign.Gemini },
            new { PeriodEnd = new DateTime(birthday.Year, 7, 22), ZodiacSign = ZodiacSign.Cancer },
            new { PeriodEnd = new DateTime(birthday.Year, 8, 21), ZodiacSign = ZodiacSign.Leo },
            new { PeriodEnd = new DateTime(birthday.Year, 9, 23), ZodiacSign = ZodiacSign.Virgo },
            new { PeriodEnd = new DateTime(birthday.Year, 10, 23), ZodiacSign = ZodiacSign.Libra },
            new { PeriodEnd = new DateTime(birthday.Year, 11, 22), ZodiacSign = ZodiacSign.Scorpio },
            new { PeriodEnd = new DateTime(birthday.Year, 12, 22), ZodiacSign = ZodiacSign.Sagittarius }
        };
        foreach (var periodEnd in periodEndList)
            if (birthday <= periodEnd.PeriodEnd)
                return periodEnd.ZodiacSign;
        return ZodiacSign.Capricorn;
    }
    static string GenerateCompatibilityString(DateTime birthday1, DateTime birthday2)
    {
        return string.Format("Compatibility of {0} with {1}", BirthdayToZodiacSign(birthday1), BirthdayToZodiacSign(birthday2));
    }
    static void Main(string[] args)
    {
        Console.WriteLine(GenerateCompatibilityString(new DateTime(2014, 1, 1), new DateTime(2014, 6, 7)));
    }
}

我还没有完全阅读你的代码(因为它似乎不完整,例如什么是ob),但这里有一个一般性的评论:

如果你有一个if else-if结构,那么只会执行一个分支。例如,在下面的例子中,只执行一个代码块(对应于第一个条件求值为true的代码块):

if (condition1)
{}
else if (condition2)
{}
else if (condition3)
{}
else
{}

如果您有几个条件可以求值为true,那么您应该使用多个if语句来代替:

if (condition1)
{}
if (condition2)
{}
if (condition3)
{}
// if you need an "else" part:
if (!condition1 && !condition2 && !condition3)
{}

下面是一个简单的类来解决这个问题:

public class Signs
{
  static List<string> SignList = new List<string>() {
    "Capricorn", "Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Not Found" };
  static List<int> StartDates = new List<int>() {
    // month * 100 + day of month
     120, 219, 320, 420, 521, 621, 722, 821, 923, 1023, 1122, 1222, 1232, 9999 // edge marker
  };
  static public string Sign(DateTime inDate)
  {
    return SignList[StartDates.TakeWhile(d => (inDate.Month*100)+inDate.Day > d).Count()];
  }
}

这里有另一个版本可能更清楚https://stackoverflow.com/a/23183480/215752