私がやろうとしているのは、テキスト ボックスからユーザー入力を取得し、それを int に変換してから使用することです。トライアンドキャッチ以外はすべて動作するようになりました。人が数字の代わりに文字を入れた場合。以下のコードでは、常に何かをキャッチします。何が何かをキャッチしているのかわかりません。私はブールテストを取り出しました。手紙を入れると、例外がスローされ、ビープ音が鳴ります。それ以外の場合は、有効な入力を待っています。私の乱雑なコードを許してください、私はまだ初心者のC#プログラマーです:D 上級者に感謝します!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
bool tone = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool test = true;
speedInput.Clear();
beep.Clear();
int beepspeed = 90;
int speed = 100;
string speedtext = this.speedInput.Text;
string beeptext = this.beep.Text;
try
{
test = true;
beepspeed = Convert.ToInt32(beeptext);
speed = Convert.ToInt32(speedtext);
}
catch (Exception)
{
MessageBox.Show("numbers above 37 only!!");
test = false;
}
if (test)
{
for (int i = 0; i < beepspeed; i++)
{
if (this.tone)
{
Random ran = new Random();
int rand = ran.Next(400, 3000);
Console.Beep(rand, speed);
}
else
{
Console.Beep(1000, speed);
}
}
}
}
private void radioButtonYes_CheckedChanged(object sender, EventArgs e)
{
this.tone = true;
}
private void radioButtonNo_CheckedChanged(object sender, EventArgs e)
{
this.tone = false;
}
}
}