如何仅在调用时运行方法?

发布于 2024-11-30 22:33:01 字数 3352 浏览 1 评论 0原文

我下面有一堆代码。但是,我遇到了一些错误,因为 Move() 和 Genius() 方法运行逻辑太多。我只想运行两个方法(如果它们由提交单击方法调用)。我该怎么做?

namespace ShotgunApp
{
    public partial class SingleGame : PhoneApplicationPage
    {
        public static class AmmoCount
        {
            public static int userAmmo = startVars.startAmmo;
            public static int geniusAmmo = startVars.startAmmo;
        }

        public static class Global
        {
            public static int lives = 1;
            public static string GeniusMove;
            public static string UserMove;
        }

        public SingleGame()
        {
            InitializeComponent();
            GeniusAmmo.Text = "ammo: " + AmmoCount.geniusAmmo;
            UserAmmo.Text = "ammo: " + AmmoCount.userAmmo;   
        }

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (((String)submit.Content) == "Submit")
            {
                Move();
                submit.Content = "Wait for Genius...";
                uReload.IsEnabled = false;
                uFire.IsEnabled = false;
                uShield.IsEnabled = false;
                Genius();
            }
            else if (((String)submit.Content) == "Go!")
            {
                GeniusSpeak.Text = "";
                OutcomeDesc.Text = "You have " + Move() + " and Genius has " + Genius();
                Outcome.Text = "ANOTHER ROUND...";
                submit.Content = "Continue";
            }
            else if (((String)submit.Content) == "Continue")
            {

                uReload.IsEnabled = true;
                uFire.IsEnabled = true;
                uShield.IsEnabled = true;
                OutcomeDesc.Text = "";
                Outcome.Text = "";
                submit.Content = "Submit";
            }
        }


        public string Move()
        { 
            if (uReload.IsChecked.HasValue && uReload.IsChecked.Value == true)
            {
                UserAmmo.Text = "ammo: " + ++AmmoCount.userAmmo;
                Global.UserMove = "reloaded";

            }
            else if (uShield.IsChecked.HasValue && uShield.IsChecked.Value == true)
            {
                Global.UserMove = "shielded";
            }
            else if (uFire.IsChecked.HasValue && uFire.IsChecked.Value == true)
            {
                UserAmmo.Text = "ammo: " + --AmmoCount.userAmmo;
                Global.UserMove = "fired";

            }
            else
            {
                submit.Content = "Enter a move!";
            }

            return Global.UserMove;
        }

        public string Genius()
        {
            GeniusSpeak.Text = "Genius has moved";
            submit.Content = "Go!";

            Random RandomNumber = new Random();
            int x = RandomNumber.Next(0, 3);

            if (x == 0)
            {
                Global.GeniusMove = "reloaded";
                GeniusAmmo.Text = "ammo: " + ++AmmoCount.geniusAmmo;
            }
            else if (x == 1)
            {
                Global.GeniusMove = "shielded";
            }
            else if (x == 2)
            {
                Global.GeniusMove = "fired";
                GeniusAmmo.Text = "ammo: " + ++AmmoCount.geniusAmmo;
            }

            return Global.GeniusMove;

        }
    }
}

I have a bunch of code below. However, I am hitting some bugs because the methods Move() and Genius() are running logic too much. I only want to two methods to run if they are being called by the submit click method. How can I do this?

namespace ShotgunApp
{
    public partial class SingleGame : PhoneApplicationPage
    {
        public static class AmmoCount
        {
            public static int userAmmo = startVars.startAmmo;
            public static int geniusAmmo = startVars.startAmmo;
        }

        public static class Global
        {
            public static int lives = 1;
            public static string GeniusMove;
            public static string UserMove;
        }

        public SingleGame()
        {
            InitializeComponent();
            GeniusAmmo.Text = "ammo: " + AmmoCount.geniusAmmo;
            UserAmmo.Text = "ammo: " + AmmoCount.userAmmo;   
        }

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (((String)submit.Content) == "Submit")
            {
                Move();
                submit.Content = "Wait for Genius...";
                uReload.IsEnabled = false;
                uFire.IsEnabled = false;
                uShield.IsEnabled = false;
                Genius();
            }
            else if (((String)submit.Content) == "Go!")
            {
                GeniusSpeak.Text = "";
                OutcomeDesc.Text = "You have " + Move() + " and Genius has " + Genius();
                Outcome.Text = "ANOTHER ROUND...";
                submit.Content = "Continue";
            }
            else if (((String)submit.Content) == "Continue")
            {

                uReload.IsEnabled = true;
                uFire.IsEnabled = true;
                uShield.IsEnabled = true;
                OutcomeDesc.Text = "";
                Outcome.Text = "";
                submit.Content = "Submit";
            }
        }


        public string Move()
        { 
            if (uReload.IsChecked.HasValue && uReload.IsChecked.Value == true)
            {
                UserAmmo.Text = "ammo: " + ++AmmoCount.userAmmo;
                Global.UserMove = "reloaded";

            }
            else if (uShield.IsChecked.HasValue && uShield.IsChecked.Value == true)
            {
                Global.UserMove = "shielded";
            }
            else if (uFire.IsChecked.HasValue && uFire.IsChecked.Value == true)
            {
                UserAmmo.Text = "ammo: " + --AmmoCount.userAmmo;
                Global.UserMove = "fired";

            }
            else
            {
                submit.Content = "Enter a move!";
            }

            return Global.UserMove;
        }

        public string Genius()
        {
            GeniusSpeak.Text = "Genius has moved";
            submit.Content = "Go!";

            Random RandomNumber = new Random();
            int x = RandomNumber.Next(0, 3);

            if (x == 0)
            {
                Global.GeniusMove = "reloaded";
                GeniusAmmo.Text = "ammo: " + ++AmmoCount.geniusAmmo;
            }
            else if (x == 1)
            {
                Global.GeniusMove = "shielded";
            }
            else if (x == 2)
            {
                Global.GeniusMove = "fired";
                GeniusAmmo.Text = "ammo: " + ++AmmoCount.geniusAmmo;
            }

            return Global.GeniusMove;

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-12-07 22:33:01

将最后的结果存储在数据成员中:

        private string lastMoveResult = string.Empty;
        private string lastGeniusResult = string.Empty;

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (((String)submit.Content) == "Submit")
            {
                lastMoveResult = Move();
                submit.Content = "Wait for Genius...";
                uReload.IsEnabled = false;
                uFire.IsEnabled = false;
                uShield.IsEnabled = false;
                lastGeniusResult = Genius();
            }
            else if (((String)submit.Content) == "Go!")
            {
                GeniusSpeak.Text = "";
                OutcomeDesc.Text = "You have " + lastMoveResult + " and Genius has " + lastGeniusResult ;
                Outcome.Text = "ANOTHER ROUND...";
                submit.Content = "Continue";
            }

Store the last results in data members:

        private string lastMoveResult = string.Empty;
        private string lastGeniusResult = string.Empty;

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (((String)submit.Content) == "Submit")
            {
                lastMoveResult = Move();
                submit.Content = "Wait for Genius...";
                uReload.IsEnabled = false;
                uFire.IsEnabled = false;
                uShield.IsEnabled = false;
                lastGeniusResult = Genius();
            }
            else if (((String)submit.Content) == "Go!")
            {
                GeniusSpeak.Text = "";
                OutcomeDesc.Text = "You have " + lastMoveResult + " and Genius has " + lastGeniusResult ;
                Outcome.Text = "ANOTHER ROUND...";
                submit.Content = "Continue";
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文