Why does this piece of code does not work?
it is supposed to be a "random walk". and I should not be using any methods or classes. just branching, looping and arrays.
I have no errors from the compiler but when I try to run the code console appears and then disappears before I can do anything.
static void Main(string[] args)
{
Random randomObject = new Random();
int randomDirection;
int row=1;
int col=1;
while (true)
{
int n;
int row = 1;
int col = 1;
Console.WriteLine("Enter the number of steps: ");
n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
randomDirection = randomObject.Next(1, 5);
if (randomDirection == 1 && row > 0 && row < 10)
row++;
else if (randomDirection == 2 && row > 0 && row < 10)
row--;
else if (randomDirection == 3 && col > 0 && col < 10)
col++;
else if (randomDirection == 4 && col > 0 && col < 10)
col--;
else
i--;
}
Console.WriteLine("You have walked in row: " + row);
Console.WriteLine("You have walked in col: " + col);
Console.WriteLine("");
}
Console.ReadKey();
}
Loading
Srinubabu RavillaPosted May 15, 2013, 12:24 AM
I have tried it. Please remove the local variable row and col and then try it as follows.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random randomObject = new Random();
int randomDirection;
int row = 1;
int col = 1;
while (true)
{
int n;
Console.WriteLine("Enter the number of steps: ");
n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
randomDirection = randomObject.Next(1, 5);
if (randomDirection == 1 && row > 0 && row < 10)
row++;
else if (randomDirection == 2 && row > 0 && row < 10)
row--;
else if (randomDirection == 3 && col > 0 && col < 10)
col++;
else if (randomDirection == 4 && col > 0 && col < 10)
col--;
else
i--;
}
Console.WriteLine("You have walked in row: " + row);
Console.WriteLine("You have walked in col: " + col);
Console.WriteLine("");
}
Console.ReadKey();
}
}
}
Jignesh TrivediPosted May 14, 2013, 11:40 PM
I have also got completion error on above code.
A local variable named 'row' cannot be declared in this scope because it would give a different meaning to 'row',
which is already used in a 'parent or current' scope to denote something
else
if we remove int declaration from row and col than it work fine.
Random randomObject = new Random();
int randomDirection;
int row = 1;
int col = 1;
while (true)
{
int n;
row = 1;
col = 1;
Console.WriteLine("Enter the number of steps: ");
n = Convert.ToInt32(Console.ReadLine());
for (int i22 = 0; i22 < n; i22++)
{
randomDirection = randomObject.Next(1, 5);
if (randomDirection == 1 && row > 0 && row < 10)
row++;
else if (randomDirection == 2 && row > 0 && row < 10)
row--;
else if (randomDirection == 3 && col > 0 && col < 10)
col++;
else if (randomDirection == 4 && col > 0 && col < 10)
col--;
else
i22--;
}
Console.WriteLine("You have walked in row: " + row);
Console.WriteLine("You have walked in col: " + col);
Console.WriteLine("");
}
Console.ReadKey();
saman hamidiPosted May 14, 2013, 1:12 PM
Javeed M ShaikhPosted May 14, 2013, 12:31 PM
while (true)
{
int n;
int row = 1;
int col = 1;
you have already declared int row and int col above this loop, change it to:
while (true)
{
int n;
row = 1;
col = 1;