【问题标题】:integer not incrementing, list not growing... values from session c# asp.net整数不递增,列表不增长...来自会话 c#asp.net 的值
【发布时间】:2017-04-05 11:34:45
【问题描述】:

我正在构建一个非常基本的“骰子板球”模拟器。我正在尝试使用我存储在 asp.net 会话中的整数和列表来跟踪已完成的局数和回合数以及得分。

我有一个“innsOver”类,代表一组 6 次投篮,每个投篮都有一个整数代表球的结果。

public class innsOver
{
public int overNumber { get; set; }
public int ball1 { get; set; }
public int ball2 { get; set; }
public int ball3 { get; set; }
public int ball4 { get; set; }
public int ball5 { get; set; }
public int ball6 { get; set; }
}

我也有 2 个基于此类的列表,代表 2 局:

List<innsOver> inns1 = new List<innsOver>();
List<innsOver> inns2 = new List<innsOver>();

我还声明了完整客栈的数量(所以我知道我是在玩客栈 1 还是客栈 2)和完整的回合数(所以我知道有多少回合被投球)。这些是

int oversComplete = 0;
int innsComplete = 0;

oversCompleteinnsComplete 2 个整数以及 inns1inns2 2 个列表是全局声明的。

“结束”是通过按下一个按钮来播放的,该按钮调用playOver() 方法,该方法本身调用bowlDelivery() 方法。当 playOver() 方法完成时,它应该增加 completeOvers 整数,将完成的结果添加到适当的局列表(inns1inns2)并将其返回到 gridView。

public void bowlOver()
{
    //other processing checks which end the over is bowled from
   //which batsman is the striker / non-striker and who is bowling.

    for (int i = 1; i <7; i++)
    {
        int x = bowlDelivery();
        int b = 0;
        if (x == 5)
        {
            //this represents a chance of a wicket that was 
            //unsuccessful so no run scored
            b = 0;
        }
        else if (x == 7)
        {
            //this means a wicket fell so i have wicket processing;
        }
        else if (x == 1 || x == 3)
        {
            b = x;
            //award 1 run & change striking batsman
        }
        else
        {
            b = x;
           //2, 4 or 6 runs awarded
        }
        //the outcome is returned to the correct ball
        switch (i)
        {
            case 1:
                activeOver.ball1 = b;
                break;
            case 2:
                activeOver.ball2 = b;
                break;
            case 3:
                activeOver.ball3 = b;
                break;
            case 4:
                activeOver.ball4 = b;
                break;
            case 5:
                activeOver.ball5 = b;
                break;
            case 6:
                activeOver.ball6 = b;
                break;
        }

    }
    //the over result is returned to the correct inns list
    switch (innsComplete)
    {
        case 0:
            Session["inns1"] = inns1;
            Session["oversComplete"] = oversComplete;
            oversComplete = (int)Session["oversComplete"];
            activeOver.overNumber = oversComplete + 1;
            oversComplete = activeOver.overNumber;
            inns1 = (List<innsOver>)Session["inns1"];
            inns1.Add(activeOver);
            GridView3.DataSource = inns1;
            GridView3.DataBind();
            Session["inns1"] = inns1;
            Session["oversComplete"] = oversComplete;
            break;
        case 1:
            Session["inns2"] = inns2;
            Session["oversComplete"] = oversComplete;
            oversComplete = (int)Session["oversComplete"];
            activeOver.overNumber = oversComplete + 1;
            oversComplete = activeOver.overNumber;
            inns2 = (List<innsOver>)Session["inns2"];
            inns2.Add(activeOver);
            GridView3.DataSource = inns2;
            GridView3.DataBind();
            Session["inns2"] = inns2;
            Session["oversComplete"] = oversComplete;
            break;
    }
}

public int bowlDelivery()
{
   //currently the result of each ball is completely random but i will add weighting later
    int x = rnd.Next(1, 7);
    if (x == 5)
    {
        int y = rnd.Next(1, 6);
        if(y > 4)
        {
            x = 7;
        }
        else
        {
            x = 5;
        }
    }
    return x;
}

但是,目前我只收到一个 over 返回,并且 overNumber 始终为“1”。球值发生变化,所以它似乎只是在重复打第一个。我期待(试图实现)overNumber 递增和额外的行被添加到显示在 gridview 中的 innsX 列表中。

对我的错误有什么建议吗?

【问题讨论】:

  • 你确定方法被调用吗?
  • 是的,因为第一轮出现在网格视图中。它只是永远不会超过第一次。
  • rnd 是如何以及在哪里初始化的?
  • 也在全球范围内使用Random rnd = new Random(); 我应该澄清一下,除了completeOvers 整数的递增和innsX 列表的行数增加之外,一切都按预期工作。
  • 您能否告诉我们方法调用发生在页面周期中的哪个事件?如果您能与我们分享完整的代码,那就太好了。某处发生了一些值重置,如果不查看完整代码,将很难找到它。

标签: c# asp.net list


【解决方案1】:

在每个case 语句的开头你有Session["inns1"] = inns1; innsX 来自哪里?它是每次都设置为 Session 的新列表吗?在这种情况下,看起来Session["innsX"] 每次都会被覆盖,因此只有一条记录可以显示在网格中。

【讨论】:

  • 您好,谢谢。我想我正在做你所说的:activeOver.overNumber = oversComplete + 1; oversComplete = activeOver.overNumber;,然后最后将新的oversComplete 存储在会话中。所以我的信念是我让activeOvercompleteOvers 多1,然后将completeOvers 设置为activeOver,这与我相信oversComplete++ 相同吗?
  • 我现在明白了。对不起。
  • 没问题!我不是专业人士,错过这样的事情绝不会像我一样! :)
  • 我没有发现您编辑的答案。 innsX 是一个全局声明的列表。所以我的理解是,它不应该是每次都发送到会话的“新” innsX ......但该代码对我来说看起来有点笨拙。我会到处玩,看看我能得到什么。谢谢。
  • -是的,我猜innsX 会话被覆盖的问题。我将innsX 列表和oversCompleteinnsComplete 整数的会话值的初始创建移至“PageLoad”,因此总的来说它所做的就是更新它们。它现在按预期工作。谢谢指点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 2016-03-07
  • 2010-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多