简体   繁体   中英

C# - Random Number Generator error

When I run this and play the game, only the number 0 is being generated each time. Can you help me figure what the problem is?

public partial class MainPage : PhoneApplicationPage
{
    int numberguessed;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        int numberguessed = randnum.Next(0,1000);
    }



    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        myTextBlock.Text = " No worries ! Go again .. ";
        myTextbox.Text = "";
        myTextbox.Focus();
    }

    private void myButton2_Click(object sender, RoutedEventArgs e)
    {
        //string sval = myTextbox.Text;
        int ival = System.Convert.ToInt32(myTextbox.Text);
        if (ival == numberguessed)
            myTextBlock.Text = " You won ";
        else if (ival < numberguessed)
            myTextBlock.Text = "Your guess is too low !";
        else if (ival > numberguessed)
            myTextBlock.Text = "Your guess is too high !";
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        myTextbox.Focus();
    }

in this part

public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        int numberguessed = randnum.Next(0,1000);
    }

You are overwritting your top level numberguessed variable by prefixing it with "int". Change it to:

public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        numberguessed = randnum.Next(0,1000);
    }

You declare numberguessed as a field , and then redeclare a new local variable int numberguessed in MainPage() . In the other methods, the field value will be used. As it's not initialized, it will have the default value for an int, 0.

int numberguessed;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Random randnum = new Random();
            //remove int there like this
            //int numberguessed = randnum.Next(0,1000);
           numberguessed = randnum.Next(0,1000);
        }

By the way you should have a warning (or maybe it's just resharper doing it) stating that

Local variable numberguessed hides fields .MainPage.numberguessed

change this part

int numberguessed;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        int numberguessed = randnum.Next(0,1000);
    }

To

 private int numberguessed;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        numberguessed = randnum.Next(0,1000);
    }

The problem is the fact that once you are declaring your field/property in the class and then creating a similar field with the declaration of int numberguessed again inside the constructor. This latter field only remains in scope inside the constructor and dies off once the constructor ends. However, the default value of all int fields are 0 and the field you are accessing is the one defined OUTSIDE the class. Hence you are getting only a default value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM