数字猜测游戏甚至在它开始之前就完成了

时间:2017-05-22 07:52:33

标签: c# ios xamarin.ios

我正在使用xamarim.ios并在下面的代码中遇到问题:

using System;

using UIKit;

namespace SingleViewApp
{
    public partial class ViewController : UIViewController
    {
        int count = 0;
        int countFrom = 8;
        int guess;
        bool run = false;

    public ViewController(IntPtr handle) : base(handle)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.

        textView.Text = $"Can you guess my number in {countFrom} or less tries? Number: ";

    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }

    partial void EnterButton_TouchUpInside(UIButton sender)
    {
        Random random = new Random();
        int number = random.Next(1, 1001);

        try
        {
            while (!run)
            {
                guess = Convert.ToInt32(guessInput.Text);

                if (guess == number && count <= 8)
                {
                    textView.Text = $"Congrats, you won! Guessed in {count + 1} guesses.";

                    run = true;
                }
                else if (guess != number && count >= 8)
                {
                    textView.Text = $"You lose! The number was {number}.";

                    run = true;
                }
                else if (guess == (number - 25))
                {
                    textView.Text = "A hint: The number is higher than that!";

                }
                else if (guess == (number + 25))
                {
                    textView.Text = "A hint: The number is lower than that!";

                }
                else if (guess <= (number - 50))
                {
                    textView.Text = "A hint: The number is higher than that!";

                }
                else if (guess == (number + 50))
                {
                    textView.Text = "A hint: The number is lower than that!";

                }
                else if (guess == (number - 75))
                {
                    textView.Text = "A hint: The number is higher than that!";

                }
                else if (guess == (number + 75))
                {
                    textView.Text = "A hint: The number is lower than that!";

                }
                else if (guess == (number - 100))
                {
                    textView.Text = "A hint: The number is higher than that!";

                }
                else if (guess == (number + 100))
                {
                    textView.Text = "A hint: The number is lower than that!";

                }
                else if (guess >= (number - 2) && guess <= (number + 2))
                {
                    textView.Text = "You're practically there (2) !";

                }
                else if (guess >= (number - 5) && guess <= (number + 5))
                {
                    textView.Text = "You're very close (5) !";

                }
                else if (guess >= (number - 13) && guess <= (number + 13))
                {
                    textView.Text = "You are close (13) !";

                }
                else if (guess >= (number - 25) && guess <= (number + 25))
                {
                    textView.Text = "It's warmer.";

                }
                else if (guess >= (number - 50) && guess <= (number + 50))
                {
                    textView.Text = "It's warm.";

                }
                else if (guess >= (number - 75) && guess <= (number + 75))
                {
                    textView.Text = "It's not too far anymore!";

                }
                else if (guess >= (number - 100) && guess <= (number + 100))
                {
                    textView.Text = "You are not quite there!";

                }
                else if (guess <= (number - 150))
                {
                    textView.Text = "You are too low!";

                }
                else if (guess >= (number + 150))
                {
                    textView.Text = "You are too high!";

                }
                else if (guess > number)
                {
                    textView.Text = "It's lower.";

                }
                else if (guess < number)
                {
                    textView.Text = "It's higher.";
                }

                count++;
                countFrom--;
            }
        }
        catch (FormatException e)
        {
            textView.Text = "Wrong input!";

        }
    }
}

}

首先,代码并不好,并且会产生可能缩短或通常会被重写的不必要的事物或事物 但我的实际问题是,为什么我的应用程序使用输入的数字进行8次运行,然后输出在游戏开始之前丢失的输出? 当第一轮比赛结束时我应该能够输入下一个数字/尝试

1 个答案:

答案 0 :(得分:1)

我从未创建过iOS应用程序,所以不知道最佳做法是什么,但我会在EnterButton之外生成数字,然后我认为你根本不需要循环。

var instanceDialogClientDonneur = null;
...
if (instanceDialogClientDonneur == null  ) {
instanceDialogClientDonneur = new BootstrapDialog.show({
        title : titre,
        cssClass : 'lieu-dialog',
        modal : true,

        closeOnEscape : true,
        onhidden : function() {             
        instanceDialogClientDonneur =null;
            },
        message : enteteZoneRecherche + resTab +"</div>"
    });
} else {
...
}