C#[4小时新增:)的新品牌],但希望在Board Feet Calculator中限制用户输入的指针仅限于数字,不允许使用字母或特殊字符。将用户输入限制为仅限数字
首先,限制发生在类,方法和/或程序中吗? (我相信类和方法)
第二,我已经看到了一个例子,我会用类似的东西吗?
第三,如果是这样,我是否需要为KeyPress和KeyPressEventArgs分别创建类? (我相信他们会自动出现,例如 public char KeyChar { get; set; }
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// allows only letters
if (!char.IsLetter(e.KeyChar))
{
e.Handled = true;
}
}
我的计划
namespace BoardFt_MyTry_
{
class Program
{
static void Main(string[] args)
{
Board board = new Board();
board.lengthOfboard = Convert.ToDouble(askQuestion("What is the length of your board in inches?"));
board.widthOfboard = Convert.ToDouble(askQuestion("What is the width of your board in inches?"));
board.thicknessOfboard = Convert.ToDouble(askQuestion("What is the thickness of your board in inches?"));
Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt());
Console.ReadLine();
}
private static string askQuestion(string question)
{
Console.WriteLine(question);
return Console.ReadLine();
}
}
我局级
namespace BoardFt_MyTry_
{
class Board
{
public double lengthOfboard;
public double widthOfboard;
public double thicknessOfboard;
public double CalcBoardFt()
{
double boardft = 0;
boardft = (this.lengthOfboard * this.widthOfboard * this.thicknessOfboard)/144;
return boardft;
}
}
}
这通常被称为“验证”。 –
不要这样做 - 让用户输入带有附加字符的数字,例如空格,逗号,+, - ,然后根据验证结果过滤输入或拒绝输入。即+1,000, - 1 000,1e3,0.1e + 4都可以,1j34不是。 –
谢谢约翰澄清它是“验证”,以及确认我需要采取不良的输入,并通知用户它是坏的。 (我会赞成,但有1名声望)。 – vayden12