Tuesday, July 28, 2009

Code problem??? Plz Help?

Hi, I have the following bit of C# code that i can't get to do what I want. Basically it is supposed to pick a number at random and this number will be put through a switch case statement which will then put the word into the text box. The problem I'm having is that I want to stop the random selector from picking the same word twice in a row so say if i = j then to do another randomnumber. However it won't do this for me. I intitialise j at the page load, if page.ispostback == false





int i;








Random RandomNumber = new Random();


i = RandomNumber.Next(1, 4);





if (i == j)


{


i = RandomNumber.Next(1, 4);


}





switch(i){


case 1:


TextBox1.Text = "WordOne";


j = 1;


break;


case 2:


TextBox1.Text = "WordTwo";


j = 2;


break;


case 3:


TextBox1.Text = "WordThree";


j = 3;


break;

Code problem??? Plz Help?
Here is an idea. Make the switch statement another sub - call it select(). It has an argument "i" which is an integer.





You need to include this sub in the if-statement like this:





Random RandomNumber = new Random();


i = RandomNumber.Next(1, 4);





if (i=j)


{


i = RandomNumber.Next(1, 4);


select(i);


}


else


select(i);
Reply:Sorry, I know C++. The C++ random number generator is: rnum=rand()%11;.
Reply:well..... x + y = turtle over jumper %26amp; socks!


hope this helps
Reply:int i,j;





i = j = 1;





Random RandomNumber = new Random();





while (i == j) {


i = RandomNumber.Next(1, 4);


}


j = i;

send flowers

No comments:

Post a Comment