C# 仅将用户输入限制为数字
对C#[4小时新:)]来说是全新的,但希望在板脚计算器上有一些指针,将用户输入限制为仅数字,不允许字母或特殊字符 首先,限制是否发生在类、方法和/或程序中?(我相信课程和方法) 其次,我看到了下面的一个例子,我会使用类似的东西吗 第三,如果是这样,我是否需要为KeyPress和KeyPressEventArgs创建单独的类?(我相信他们会自动到那里去。C# 仅将用户输入限制为数字,c#,validation,C#,Validation,对C#[4小时新:)]来说是全新的,但希望在板脚计算器上有一些指针,将用户输入限制为仅数字,不允许字母或特殊字符 首先,限制是否发生在类、方法和/或程序中?(我相信课程和方法) 其次,我看到了下面的一个例子,我会使用类似的东西吗 第三,如果是这样,我是否需要为KeyPress和KeyPressEventArgs创建单独的类?(我相信他们会自动到那里去。 publiccharkeychar{get;set;} private void textBox1_KeyPress(object sender
publiccharkeychar{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;
}
}
}
你不能在控制台应用程序中真正做到这一点。你所能做的就是允许用户输入坏数据,然后告诉用户数据坏了 您可以尝试以下方法:
class Program
{
public double AskDnoubleQuestion(string message){
do {
Console.Write(message);
var input = Console.ReadLine();
if (String.IsNullOrEmpty(input)){
Console.WriteLine("Input is required");
continue;
}
double result;
if (!double.TryParse(input, out result)){
Console.WriteLine("Invalid input - must be a valid double");
continue;
}
return result;
}
static void Main(string[] args)
{
Board board = new Board();
board.lengthOfboard = AskDoubleQuestion("What is the length of your board in inches?");
board.widthOfboard = AskDoubleQuestion(askQuestion("What is the width of your board in inches?");
board.thicknessOfboard = AskDoubleQuestion(askQuestion("What is the thickness of your board in inches?");
Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt());
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
string number = ReadNumber();
Console.WriteLine("You entered: " + number);
}
private static string ReadNumber()
{
string input = "";
do
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (char.IsNumber(keyInfo.KeyChar))
{
input = input + keyInfo.KeyChar;
Console.Write(keyInfo.KeyChar);
}
if (keyInfo.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (keyInfo.Key == ConsoleKey.Backspace)
{
input = input.Substring(0, input.Length - 1);
Console.Write("\b \b");
}
} while (true);
return input;
}
}
string input = ReadOnlyNumbers();
Console.WriteLine(input);
你不能在控制台应用程序中真正做到这一点。你所能做的就是允许用户输入坏数据,然后告诉用户数据坏了 您可以尝试以下方法:
class Program
{
public double AskDnoubleQuestion(string message){
do {
Console.Write(message);
var input = Console.ReadLine();
if (String.IsNullOrEmpty(input)){
Console.WriteLine("Input is required");
continue;
}
double result;
if (!double.TryParse(input, out result)){
Console.WriteLine("Invalid input - must be a valid double");
continue;
}
return result;
}
static void Main(string[] args)
{
Board board = new Board();
board.lengthOfboard = AskDoubleQuestion("What is the length of your board in inches?");
board.widthOfboard = AskDoubleQuestion(askQuestion("What is the width of your board in inches?");
board.thicknessOfboard = AskDoubleQuestion(askQuestion("What is the thickness of your board in inches?");
Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt());
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
string number = ReadNumber();
Console.WriteLine("You entered: " + number);
}
private static string ReadNumber()
{
string input = "";
do
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (char.IsNumber(keyInfo.KeyChar))
{
input = input + keyInfo.KeyChar;
Console.Write(keyInfo.KeyChar);
}
if (keyInfo.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (keyInfo.Key == ConsoleKey.Backspace)
{
input = input.Substring(0, input.Length - 1);
Console.Write("\b \b");
}
} while (true);
return input;
}
}
string input = ReadOnlyNumbers();
Console.WriteLine(input);
如果验证不是您想要的方式,您可以执行以下操作:
class Program
{
public double AskDnoubleQuestion(string message){
do {
Console.Write(message);
var input = Console.ReadLine();
if (String.IsNullOrEmpty(input)){
Console.WriteLine("Input is required");
continue;
}
double result;
if (!double.TryParse(input, out result)){
Console.WriteLine("Invalid input - must be a valid double");
continue;
}
return result;
}
static void Main(string[] args)
{
Board board = new Board();
board.lengthOfboard = AskDoubleQuestion("What is the length of your board in inches?");
board.widthOfboard = AskDoubleQuestion(askQuestion("What is the width of your board in inches?");
board.thicknessOfboard = AskDoubleQuestion(askQuestion("What is the thickness of your board in inches?");
Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt());
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
string number = ReadNumber();
Console.WriteLine("You entered: " + number);
}
private static string ReadNumber()
{
string input = "";
do
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (char.IsNumber(keyInfo.KeyChar))
{
input = input + keyInfo.KeyChar;
Console.Write(keyInfo.KeyChar);
}
if (keyInfo.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (keyInfo.Key == ConsoleKey.Backspace)
{
input = input.Substring(0, input.Length - 1);
Console.Write("\b \b");
}
} while (true);
return input;
}
}
string input = ReadOnlyNumbers();
Console.WriteLine(input);
这将允许用户只输入数字。如果愿意,您可以按任何方式筛选它。例如,仅输入字母和数字等
目前,它只允许整数。如果要允许小数点,请将上面的行更改为:
If(char.IsNumber(keyInfo.KeyChar)| | keyInfo.KeyChar==”)
如果验证不是您想要进行的方式,您可以这样做:
class Program
{
public double AskDnoubleQuestion(string message){
do {
Console.Write(message);
var input = Console.ReadLine();
if (String.IsNullOrEmpty(input)){
Console.WriteLine("Input is required");
continue;
}
double result;
if (!double.TryParse(input, out result)){
Console.WriteLine("Invalid input - must be a valid double");
continue;
}
return result;
}
static void Main(string[] args)
{
Board board = new Board();
board.lengthOfboard = AskDoubleQuestion("What is the length of your board in inches?");
board.widthOfboard = AskDoubleQuestion(askQuestion("What is the width of your board in inches?");
board.thicknessOfboard = AskDoubleQuestion(askQuestion("What is the thickness of your board in inches?");
Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt());
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
string number = ReadNumber();
Console.WriteLine("You entered: " + number);
}
private static string ReadNumber()
{
string input = "";
do
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (char.IsNumber(keyInfo.KeyChar))
{
input = input + keyInfo.KeyChar;
Console.Write(keyInfo.KeyChar);
}
if (keyInfo.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (keyInfo.Key == ConsoleKey.Backspace)
{
input = input.Substring(0, input.Length - 1);
Console.Write("\b \b");
}
} while (true);
return input;
}
}
string input = ReadOnlyNumbers();
Console.WriteLine(input);
这将允许用户只输入数字。如果愿意,您可以按任何方式筛选它。例如,仅输入字母和数字等
目前,它只允许整数。如果要允许小数点,请将上面的行更改为:
If(char.IsNumber(keyInfo.KeyChar)| | keyInfo.KeyChar==”)
您可以编写一个逐键读取的方法(不显示在控制台中),忽略非数字字符,打印有效字符并将其附加到StringBuilder
实例,如下所示:
public static string ReadOnlyNumbers()
{
StringBuilder input = new StringBuilder();
ConsoleKeyInfo ckey;
while ((ckey = Console.ReadKey(true)).Key != ConsoleKey.Enter)
{
if (Char.IsDigit(ckey.KeyChar))
{
Console.Write(ckey.KeyChar);
input.Append(ckey.KeyChar);
}
if (ckey.Key == ConsoleKey.Backspace)
{
input.Length--;
Console.Write("\b \b");
}
}
Console.Write(Environment.NewLine);
return input.ToString();
}
然后您可以这样使用它:
class Program
{
public double AskDnoubleQuestion(string message){
do {
Console.Write(message);
var input = Console.ReadLine();
if (String.IsNullOrEmpty(input)){
Console.WriteLine("Input is required");
continue;
}
double result;
if (!double.TryParse(input, out result)){
Console.WriteLine("Invalid input - must be a valid double");
continue;
}
return result;
}
static void Main(string[] args)
{
Board board = new Board();
board.lengthOfboard = AskDoubleQuestion("What is the length of your board in inches?");
board.widthOfboard = AskDoubleQuestion(askQuestion("What is the width of your board in inches?");
board.thicknessOfboard = AskDoubleQuestion(askQuestion("What is the thickness of your board in inches?");
Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt());
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
string number = ReadNumber();
Console.WriteLine("You entered: " + number);
}
private static string ReadNumber()
{
string input = "";
do
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (char.IsNumber(keyInfo.KeyChar))
{
input = input + keyInfo.KeyChar;
Console.Write(keyInfo.KeyChar);
}
if (keyInfo.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (keyInfo.Key == ConsoleKey.Backspace)
{
input = input.Substring(0, input.Length - 1);
Console.Write("\b \b");
}
} while (true);
return input;
}
}
string input = ReadOnlyNumbers();
Console.WriteLine(input);
您可以编写一个方法,逐键读取(不显示在控制台中),忽略非数字字符,打印有效字符并将其附加到
StringBuilder
实例,如下所示:
public static string ReadOnlyNumbers()
{
StringBuilder input = new StringBuilder();
ConsoleKeyInfo ckey;
while ((ckey = Console.ReadKey(true)).Key != ConsoleKey.Enter)
{
if (Char.IsDigit(ckey.KeyChar))
{
Console.Write(ckey.KeyChar);
input.Append(ckey.KeyChar);
}
if (ckey.Key == ConsoleKey.Backspace)
{
input.Length--;
Console.Write("\b \b");
}
}
Console.Write(Environment.NewLine);
return input.ToString();
}
然后您可以这样使用它:
class Program
{
public double AskDnoubleQuestion(string message){
do {
Console.Write(message);
var input = Console.ReadLine();
if (String.IsNullOrEmpty(input)){
Console.WriteLine("Input is required");
continue;
}
double result;
if (!double.TryParse(input, out result)){
Console.WriteLine("Invalid input - must be a valid double");
continue;
}
return result;
}
static void Main(string[] args)
{
Board board = new Board();
board.lengthOfboard = AskDoubleQuestion("What is the length of your board in inches?");
board.widthOfboard = AskDoubleQuestion(askQuestion("What is the width of your board in inches?");
board.thicknessOfboard = AskDoubleQuestion(askQuestion("What is the thickness of your board in inches?");
Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt());
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
string number = ReadNumber();
Console.WriteLine("You entered: " + number);
}
private static string ReadNumber()
{
string input = "";
do
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (char.IsNumber(keyInfo.KeyChar))
{
input = input + keyInfo.KeyChar;
Console.Write(keyInfo.KeyChar);
}
if (keyInfo.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
if (keyInfo.Key == ConsoleKey.Backspace)
{
input = input.Substring(0, input.Length - 1);
Console.Write("\b \b");
}
} while (true);
return input;
}
}
string input = ReadOnlyNumbers();
Console.WriteLine(input);
这通常被称为“验证”。不要这样做-允许用户输入带有附加字符的数字,例如空格、逗号、+、-,然后根据验证结果过滤输入或拒绝输入。例如,+1000、-1000、1e3、0.1e+4可以,1j34不可以。感谢John澄清这两个是“验证”,以及确认我需要接受错误输入并通知用户它是错误的。(我会投票,但有1个声誉)。谢谢Danny,你也确认了同样的,允许用户输入,并返回一个错误以确认错误输入。这通常称为“验证”。不要这样做-允许用户输入带有附加字符的数字,例如空格、逗号、+、-,然后根据验证结果筛选输入或拒绝输入。例如,+1000、-1000、1e3、0.1e+4可以,1j34不可以。感谢John澄清这两个是“验证”,以及确认我需要接受错误输入并通知用户它是错误的。(我会投票,但有1个声誉)。谢谢Danny,你也确认了相同的,允许用户输入,并返回一个错误以确认错误输入。