小编bkr*_*bbs的帖子

负整数除法令人惊讶的结果

在我的应用程序中,我遇到了以下结果,并对结果感到惊讶:

8/-7=-2 (两个整数).

这意味着什么?

python division integer-division

42
推荐指数
3
解决办法
2万
查看次数

c#检查密钥是否存在于字典中,然后传递其值

在我的桌面C#应用程序中,我从字典开始.我希望能够检查这个字典中的密钥.如果字典有这个键,我想把它传递给一个方法.如果字典没有这个键,我想创建一个空白列表,然后将其传递给它.我怎样才能做到这一点?

我收到错误"给定密钥不在字典中".我可以添加一个默认值,因此它永远不会为null吗?

// myDic was declared as a Dictionary<string, List<string>    

// Here is how I call someFunction
string text = SomeFunction(stringValue1, stringValue2, myDic[field1.field2]);

// SomeFunction looks like this
string SomeFunction (string string1, string string2, List<string> ra) 
{  
     // method 
     return stringResult;
} 
Run Code Online (Sandbox Code Playgroud)

c# dictionary

9
推荐指数
3
解决办法
3万
查看次数

在.NET图表中仅使用X轴中的自定义标签

我在C#中创建一个.NET线图,X轴间隔为几周.对于我的项目,我只想使用自定义标签,但是现在我仍然需要网格线.有没有人知道在保留自定义标签的同时隐藏默认X轴标签的方法?

我试过这个:

Chart4.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
Run Code Online (Sandbox Code Playgroud)

显而易见的结果是没有应用标签,这不是我想要做的.

编辑: 生成原始行的代码是这样的:

Chart4.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "M";
Run Code Online (Sandbox Code Playgroud)

自定义标签的代码是这样的:

int month = XValues[0].Month;
var XAxis = Chart4.ChartAreas[0].AxisX;

DateTime StartMonthPos = XValues[0];
DateTime EndPos = new DateTime();

foreach (DateTime Date in XValues)
{
    EndPos = Date;

    if (Date.Month != month)
    {
        Chart4.ChartAreas[0].AxisX.CustomLabels.Add(StartMonthPos.ToOADate(), EndPos.ToOADate(), StartMonthPos.ToString("MMMM"), 1, LabelMarkStyle.None);
        StartMonthPos = Date;
    }

    month = Date.Month;
}

XAxis.CustomLabels.Add(StartMonthPos.ToOADate(), EndPos.ToOADate(), StartMonthPos.ToString("MMMM"), 1, LabelMarkStyle.None);
Run Code Online (Sandbox Code Playgroud)

该图表如下所示: 带有日期的图表

它看起来应该是这样的: 没有约会的图表

.net c# charts linechart axis-labels

6
推荐指数
1
解决办法
1万
查看次数

C#Linq List <string> takewhile

请查看Linqpad中的以下代码,并告诉我为什么它返回0项而不是1项.

void Main()
{
    string[] strArray = {"apple", "banana", "cherry", "e"};
    List<string> lst = strArray.ToList();

    //count all occurences of variable alphabet "e" in LINQ

    //tip is to get the occurences of letter "e" in each word
    // and then total them together

    var lst2 = lst.TakeWhile(c=>c.Equals("banana")).Select(c=>c);

    Console.WriteLine(lst2);
}
Run Code Online (Sandbox Code Playgroud)

上面的代码没有像我期望的那样在linqpad中返回1个项目.相反,它返回0项.带有1个项目"banana"的列表应该返回.为什么不呢?

c# linq list

3
推荐指数
1
解决办法
1350
查看次数

岩纸剪刀胜率不起作用?

计算机和播放器的赢率始终为0%或100%,我不知道为什么.我已经逐步完成了这个问题,似乎没有任何问题,但它没有像我期望的那样工作,因此必定存在某些问题.

abstract class Participant
{
    public int wins;
    float _winRate;

    protected float winRate {
        get
        {
            return _winRate;
        }
        set 
        {
            if (value < 0 || value > 100) 
            {
                throw new Exception("value cannot be less than 0 or greater than 100");
            }
            _winRate = value;
        }
    }
    public void PrintWinRate()
    {
        winRate = (wins / Game_Info.gamesPlayed) * 100;
        string _winRate = "win rate: " + winRate.ToString() + "%";
        Console.WriteLine(_winRate.PadLeft(0));
    }

    public abstract string Choice();
}

class Computer:Participant
{
    string[] …
Run Code Online (Sandbox Code Playgroud)

c# visual-studio-2010 visual-studio

1
推荐指数
1
解决办法
160
查看次数