And*_*ndB 2 c# textbox while-loop
这基本上就是我想要做的.我想允许某人输入他们想要运行特定程序的次数.我无法弄清楚的是如何将数字10改为(textBox1.Text).如果您有更好的方法,请告诉我.我是编程新手.
int counter = 1;
while ( counter <= 10 )
{
Process.Start("notepad.exe");
counter = counter + 1;
}
Run Code Online (Sandbox Code Playgroud)
这显示了如何获取用户提供的输入并将其安全地转换为整数(System.Int32)并在计数器中使用它.
int counter = 1;
int UserSuppliedNumber = 0;
// use Int32.TryParse, assuming the user may enter a non-integer value in the textbox.
// Never trust user input.
if(System.Int32.TryParse(TextBox1.Text, out UserSuppliedNumber)
{
while ( counter <= UserSuppliedNumber)
{
Process.Start("notepad.exe");
counter = counter + 1; // Could also be written as counter++ or counter += 1 to shorten the code
}
}
else
{
MessageBox.Show("Invalid number entered. Please enter a valid integer (whole number).");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1401 次 |
| 最近记录: |