方法显示不正确的结果 [英] Method Displays Incorrect Results

查看:134
本文介绍了方法显示不正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过使用调试程序步骤能正常工作并显示正确的结果。然而,当我运行程序它显示homeTeamRuns和awayTeamRuns一些非常高的数字。我似乎无法找出问题。

 类球赛
{
//字段
私人字符串输出=;

INT超时= 0;
INT运行= 0;

INT homeTeamRuns = 0;
INT awayTeamRuns = 0;

布尔homeTeam = FALSE;
布尔awayTeam = FALSE;

INT [] =局新INT [12]; //安监局

//属性
公共字符串输出
{
得到
{
返回输出;
}

{
输出=值;
}
}

//游戏模拟器
公共无效playBall()
{

INT [] =打新INT [4]; //基地



的for(int i = 1; I< = 2;我++)
{
homeTeam = FALSE;
awayTeam = FALSE;



如果(我%2 == 0)
homeTeam = TRUE;
,否则
awayTeam = TRUE;

//半埋立
,而(超时3;)
{

布尔yourOut =假,单=假,double1 =假,三=假,本垒打= FALSE;

随机rnd1 =新的随机();
INT randomNum = rnd1.Next(1,101);

//
如果(randomNum> = 1&安培;&安培; randomNum< = 50)// 50%的几率
yourOut = TRUE;
否则如果(randomNum大于50&安培;&安培; randomNum< = 60)// 10%的几率
单= TRUE;
,否则如果(randomNum→60&放大器;&放大器; randomNum&下; = 92)// 42%的机会
double1 =真;
否则如果(randomNum> 92安培;&安培; randomNum< = 97)// 5%的几率
三联= TRUE;
否则如果(randomNum> 97安培;&安培; randomNum< = 100)//%几率3
本垒打= TRUE;


如果(yourOut ==真)
{
超时++;
}
,否则如果(单==真)
{
Shift键(播放);
runScored(播放);

播放[0] = 1;
}
,否则如果(double1 ==真)
{
Shift键(播放);
runScored(播放);

Shift键(播放);
runScored(播放);

播放[1] = 1;
}
,否则,如果(三==真)
{
Shift键(播放);
runScored(播放);

Shift键(播放);
runScored(播放);

Shift键(播放);
runScored(播放);

播放[2] = 1;
}
,否则如果(本垒打==真)
{
Shift键(播放);
runScored(播放);

Shift键(播放);
runScored(播放);

Shift键(播放);
runScored(播放);

Shift键(播放);
runScored(播放);

播放[3] = 1;
}


播放[3] = 0;

如果(奏== 3)
{
播放[0] = 0;
播放[1] = 0;
播放[2] = 0;
播放[3] = 0;
}
}

超时= 0;

}
输出=主队运行:+ homeTeamRuns +客队运行:+ awayTeamRuns;

}

//移动数组
公众诠释[] Shift键(INT []数组),
{
的for(int i = array.Length -1; I> = 1;我 - )
{
数组[我] =阵列[我 - 1];
}
数组[0] = 0;

返回数组;
}

公共无效runScored(INT []数组),
{
如果(阵列[3] == 1)
{
如果(awayTeam ==真)
awayTeamRuns ++;
,否则如果(homeTeam ==真)
homeTeamRuns ++;
}
}


}


解决方案

您是在您的每次循环创建随机类的新实例。该构造的文档你使用说道:




初始化Random类的新实例,使用时间相关的默认种子值




由于循环执行速度非常快,当你调试的不是你最终使用相同的种子上多次重复使用,因此调用下一页每次返回相同的值。如果该值恰好是这将增加分数,然后将比分环路的多次迭代递增的值。



移动行



 随机rnd1 =新的随机(); 



你上面的循环将构造一个随机实例,然后为每个调用下一页一个新的随机数。



在调试器中运行时的时钟通过创建一个新的随机实例,因此种子改变了时间上的移动你不能重现此



随机实例返回相同值的更多信息可以在的this 计算器职位。此外,乔恩斯基特有博客文章使用随机类,这是非常值得的时候覆盖的陷阱读


When I step through the program using the debugger it works fine and the displays the correct results. However, when I run the program it displays some extremely high number for homeTeamRuns and awayTeamRuns. I can't seem to figure out the problem.

class BallGame
    {
        //Fields
        private string output = ""; 

        int outs = 0;
        int runs = 0;

        int homeTeamRuns = 0;
        int awayTeamRuns = 0;

        bool homeTeam = false;
        bool awayTeam = false;

        int[] innings = new int[12]; //Innings 

        //Properties
         public string Output
         {
             get
             {
                 return output;
             }
             set
             {
                 output = value;
             }
         }

        //Game Simulator
        public void playBall()
        {

            int[] play = new int[4];  //Bases



            for(int i = 1; i <= 2; i++)
            {
                homeTeam = false;
                awayTeam = false;



                if(i%2 == 0)
                    homeTeam = true;
                else 
                    awayTeam = true;

            //Half Inning
            while (outs < 3)
            {

                bool yourOut = false, single = false, double1 = false, triple = false, homerun = false;

                Random rnd1 = new Random();
                int randomNum = rnd1.Next(1, 101);

                //
                if (randomNum >= 1 && randomNum <= 50) //50% chance
                    yourOut = true;
                else if (randomNum > 50 && randomNum <= 60) //10% chance
                    single = true;
                else if (randomNum > 60 && randomNum <= 92) //42% chance
                    double1 = true;
                else if (randomNum > 92 && randomNum <= 97) //5% chance
                    triple = true;
                else if (randomNum > 97 && randomNum <= 100) //%3 chance
                    homerun = true;


                if (yourOut == true)
                {
                    outs++;
                }
                else if (single == true)
                {
                    Shift(play);
                    runScored(play);

                    play[0] = 1;
                }
                else if (double1 == true)
                {
                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    play[1] = 1;
                }
                else if (triple == true)
                {
                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    play[2] = 1;
                }
                else if (homerun == true)
                {
                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    Shift(play);
                    runScored(play);

                    play[3] = 1;
                }


                play[3] = 0;

                if (outs == 3)
                {
                    play[0] = 0;
                    play[1] = 0;
                    play[2] = 0;
                    play[3] = 0;
                }
            }

            outs = 0;

            }
            output = "Home Team Runs: " + homeTeamRuns + " Away Team Runs: " + awayTeamRuns;

         }

        //Shift Array
        public int[] Shift(int[] array)
        {
            for (int i = array.Length -1; i >= 1; i--)
            {
                array[i] = array[i - 1];
            }
            array[0] = 0;

            return array;
        }

        public void runScored(int[] array)
        {
            if(array[3] == 1)
            {
                if(awayTeam == true)
                    awayTeamRuns++;
                else if(homeTeam == true)
                    homeTeamRuns++;
            }
        }


    }

解决方案

You are creating a new instance of the Random class on each iteration of your loop. The documentation for the constructor you are using says:

Initializes a new instance of the Random class, using a time-dependent default seed value.

As the loop executes very quickly when you aren't debugging you end up with the same seed being used on many iterations and thus the call to Next returns the same value each time. If that value happens to be a value that will increment the score then the score will be incremented on many iterations of the loop.

Moving the line

Random rnd1 = new Random();

above your for loop will construct one Random instance and then create a new random number for each call to Next.

You can't reproduce this when running in the debugger as the clock has moved on by the time you create a new Random instance and thus the seed has changed.

More information on the Random instance returning the same value can be found in this StackOverflow post. Also, Jon Skeet has a blog post covering pitfalls when using the Random class which is well worth a read.

这篇关于方法显示不正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆