嗨大家寻找一个解决方案,使用用户输入来查找基于用户输入的特定数组值的方法,这是我的用户输入和数组的代码,这是一个二维数组:
public int[,] ToysMade = new int[4, 5];
public String[] days = new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
//retrieve method based from user input
public void retrieveDay()
{
String input1;
String input2;
int num;
input1 = Microsoft.VisualBasic.Interaction.InputBox("Please enter week number: ", "Enter Value");
input2 = Microsoft.VisualBasic.Interaction.InputBox("Please enter day number: ", "Enter Value");
try
{
while (!(int.TryParse(input1, out num)))
{
}
}
catch(Exception e)
{
}
txtOutput.Text += "\r\nRetrieve products completed on a specific day \r\nProducts Completed on that day are: " + " \r\n";
}
//Get user input method via microsoft.vb inputbox
public void UserInput()
{
String value;
int numCount;
//retrieveing length of ToysMade array dimensions if less than 0 repeat untill last dimension filled
for (int i = 0; i < ToysMade.GetLength(0); i++)
{
for (int j = 0; j < ToysMade.GetLength(1); j++)
{
//taking user input for array dimension 0 "first dimension" then increment it by 1 after input ready for next dimension "i + 1"
value = Microsoft.VisualBasic.Interaction.InputBox("Enter Value For " + days[j] + " of week " + (i + 1).ToString() + " Enter Value");
try
{
//making sure for only int past through
while (!(int.TryParse(value, out numCount)))
{
MessageBox.Show("Not a valid number, please try again.");
value = Microsoft.VisualBasic.Interaction.InputBox("Enter Value for " + days[i] + " of week " + (i + 1).ToString() + " Enter Value");
}
// taking values enterd from user and set next dimension for next input by incrementing it
ToysMade[i, j] = numCount;
}
catch (Exception e)
{
MessageBox.Show("Value enterd is not in a valid format");
}
}
}
}