Random number problem

• Question

• I am trying to creat a game like the over or under game but every time i call the methodthat cantains the random number it stays at zero and i have no idea why.

Program.cs

static void Main(string[] args)
{
OverUnder I = new OverUnder();
Console.WriteLine("Welcome to the over or under game.");
Console.WriteLine("\n" + I.Player());
//Console.WriteLine(I.Number());
I.Turn();

}

The other class

OverUnder.cs

private double number;
public static Random random = new Random();
private double guess;

//Generating the number the player has to guess
//Method for Number
public double Number()
{
number = random.Next(1, 101);
return number;
}

public double Num
{
get
{
return number;
}
set
{
number = value;
}

}

//Asks the player to try to guess the number
//Method for Player
public double Player()
{
Console.WriteLine("Try to guess the number between 1 and 100.");
double guess = Convert.ToDouble(input);
return guess;
}

public double Play
{
get
{
return number;
}
set
{
if (value < 0 || value > 100)
{
guess = 50;
}
else
{
guess = value;
}
}
}

public void Turn()
{
if (guess < number)
{
Console.WriteLine("LOW");
}
else if (guess > number)
{
Console.WriteLine("HIGH");
}
else if (guess == number)
{
Console.WriteLine("CORRECT you have guessed the number");
}
}

Thursday, December 12, 2013 6:39 PM

• Yo uhave a local variable and a global variable with the same name.  Make change below

```From : double guess = Convert.ToDouble(input);
To   : guess = Convert.ToDouble(input);```

jdweng

• Proposed as answer by Thursday, December 19, 2013 9:12 AM
• Marked as answer by Friday, December 20, 2013 5:03 AM
Thursday, December 12, 2013 7:32 PM
• The method in your code that generates the random number is called Number().  I cannot see anywhere in the code that you call this method.  You say that you call the method - perhaps I my eyes are failing me but I cannot see where that happens.

The logic of your program needs a lot of work.  The basic flow should be something like

1. Welcome the player and explain the rules
2. Generate the number which is to be guessed
3. Get a guess
4. while the guess does not equal the number
1. Tell player if higher or lower
2. Get a guess
5. Finished

Paul Linton

• Proposed as answer by Thursday, December 19, 2013 9:12 AM
• Marked as answer by Friday, December 20, 2013 5:03 AM
Thursday, December 12, 2013 9:31 PM

All replies

• Yo uhave a local variable and a global variable with the same name.  Make change below

```From : double guess = Convert.ToDouble(input);
To   : guess = Convert.ToDouble(input);```

jdweng

• Proposed as answer by Thursday, December 19, 2013 9:12 AM
• Marked as answer by Friday, December 20, 2013 5:03 AM
Thursday, December 12, 2013 7:32 PM
• The method in your code that generates the random number is called Number().  I cannot see anywhere in the code that you call this method.  You say that you call the method - perhaps I my eyes are failing me but I cannot see where that happens.

The logic of your program needs a lot of work.  The basic flow should be something like

1. Welcome the player and explain the rules
2. Generate the number which is to be guessed
3. Get a guess
4. while the guess does not equal the number
1. Tell player if higher or lower
2. Get a guess
5. Finished

Paul Linton

• Proposed as answer by Thursday, December 19, 2013 9:12 AM
• Marked as answer by Friday, December 20, 2013 5:03 AM
Thursday, December 12, 2013 9:31 PM
• Friday, December 13, 2013 2:02 AM