Windows Develop Bookmark and Share   
 index > Windows Forms General > While loop problem
 

While loop problem

Is there any way to add a while loop into this program i try alot of time but there is alway error when the loop reach intinput cause it is empty ???

intcount = lstguess.Items.Count;

intinput = Int32.Parse(txtInput.Text);

intdigit1 = intinput / 1000;

intdigit2 = intinput / 100 % 10;

intdigit3 = intinput / 10 % 10;

intdigit4 = intinput % 10;

lblNumOne.Text = Convert.ToString(intdigit1);

lblNumTwo.Text = Convert.ToString(intdigit2);

lblNumThree.Text = Convert.ToString(intdigit3);

lblNumFour.Text = Convert.ToString(intdigit4);

if (intrandom1 == intdigit1)

{

strnum1 = "0";

}

else if (intdigit1 == intrandom2 || intdigit1 == intrandom3 || intdigit1 == intrandom4)

{

strnum1 = "p";

}

else

{

strnum1 = "x";

}

if (intrandom2 == intdigit2)

{

strnum2 = "0";

}

else if (intdigit2 == intrandom1 || intdigit2 == intrandom3 || intdigit2 == intrandom4)

{

strnum2 = "p";

}

else

{

strnum2 = "x";

}

if (intrandom3 == intdigit3)

{

strnum3 = "0";

}

else if (intdigit3 == intrandom2 || intdigit3 == intrandom1 || intdigit3 == intrandom4)

{

strnum3 = "p";

}

else

{

strnum3 = "x";

}

if (intrandom4 == intdigit4)

{

strnum4 = "0";

}

else if (intdigit4 == intrandom2 || intdigit4 == intrandom3 || intdigit4 == intrandom1)

{

strnum4 = "p";

}

else

{

strnum4 = "x";

}

if (intrandom1 == intdigit1)

{

lblrandom1.Text = Convert.ToString(intrandom1);

}

else

{

lblrandom1.Text = "x";

}

if (intrandom1 == intdigit1)

{

lblrandom1.Text = Convert.ToString(intrandom1);

}

else

{

lblrandom1.Text = "x";

}

lstguess.Items.Add(txtInput.Text + "\t\t" + strnum1 + "\t\t" + strnum2 + "\t\t" + strnum3 + "\t\t" + strnum4);

txtInput.Clear();

txtInput.Focus();

wookie85  Monday, July 16, 2007 9:44 AM

You can do a FOR loop for your data validation

First use array

int[] intdigit = new int[4];


intdigit[0] = intinput / 1000;
intdigit[1] = intinput / 100 % 10;
intdigit[2] = intinput / 10 % 10;
intdigit[3] = intinput % 10;

Use the same technique for strnum and intrandom

Then it's possible to to a for loop to validate your data (this way you also have the ability to generate number the size you want)

It seem like a windows app, you won't be able to use a loop to handle the number of tries. You will have to keep the number of tries in a global variable. If this number exceed the maximum number of tries then don't go in your code.

if(number_of_tries >= MAX_NUMBER_OF_TRIES)
{
// Show a message box
// Disable some control or what ever
}
else
{
// data validation
}

ThE_lOtUs  Tuesday, July 17, 2007 1:26 PM

Hi, wookie85,

Could you tell us what do your codes do?

More explanation would be helpful for code analysis.

Regards.

Yu Guo â€?MSFT  Tuesday, July 17, 2007 9:38 AM
my code is a number guessing game something ike a mastermind, it will generate a random number for u to guess i hope to add a while loop to limit the number of guess
wookie85  Tuesday, July 17, 2007 10:21 AM

You can do a FOR loop for your data validation

First use array

int[] intdigit = new int[4];


intdigit[0] = intinput / 1000;
intdigit[1] = intinput / 100 % 10;
intdigit[2] = intinput / 10 % 10;
intdigit[3] = intinput % 10;

Use the same technique for strnum and intrandom

Then it's possible to to a for loop to validate your data (this way you also have the ability to generate number the size you want)

It seem like a windows app, you won't be able to use a loop to handle the number of tries. You will have to keep the number of tries in a global variable. If this number exceed the maximum number of tries then don't go in your code.

if(number_of_tries >= MAX_NUMBER_OF_TRIES)
{
// Show a message box
// Disable some control or what ever
}
else
{
// data validation
}

ThE_lOtUs  Tuesday, July 17, 2007 1:26 PM

You can use google to search for other answers

Custom Search

More Threads

• How to group items for common property change in a form?
• Raw stretching of image
• How to create a little nifty gallery panel with fluid animation?
• Custom Control canfocus
• Best Method of refreshing DataGrid?
• MFC's ScrollView equivalent in .NET
• Creating numeric only text box.
• Sizing the split
• How to find version of file programatically in .Net 2005?
• Browse through list of databases on server