当前位置:编程学习 > C#/ASP.NET >>

控制台输入只能是数字

如题,在控制台程序中提示输入 学生人数,只能输入数字 且不能为0,不使用报错的方式解决,能不能禁止输入字母或其他,就是除了按数字键,其他都没有反应!
求解
给出要求解决的代码
            Console.Write("请输入班级人数:   students=");
            int acount_students =Convert.ToInt32( Console.ReadLine());
student=           //只能输入数字 --------------------编程问答-------------------- 兄弟,别纠结这种问题了。
我当年也犯过和你类似的问题。这个没必要非要限制。
你还是在继续学习下知识吧,这问题以后就不是问题了。 --------------------编程问答-------------------- private void txtmoney_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 46 || e.KeyChar == 8)
            {
                return;
            }
            else
            {
                e.KeyChar = (char)Keys.None;
                return;
            }
        } --------------------编程问答-------------------- private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  {
   if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))
   {
     e.Handled = true;
   }
  }
或者
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  {
   if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))
   {
     e.Handled = true;
   }
  } --------------------编程问答--------------------
int acount_students;
do
{
    Console.WriteLine("请输入班级人数: students=");
}while(!int.TryParse(Console.ReadLine(),out acount_students));
//acount_students一定数字了
--------------------编程问答--------------------
引用 3 楼 thedolphin 的回复:
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  {
   if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))
   {
     e.Handled = true;
   }
  }
或者
private void TextBox_KeyPre……


正解 --------------------编程问答-------------------- 判断一下就行了
using System.Text.RegularExpressions;
bool isnum= Regex.IsMatch(Console.ReadLine(),@"^[1-9]\d*"); --------------------编程问答--------------------
引用 4 楼 wuyazhe 的回复:
C# code
int acount_students;
do
{
    Console.WriteLine("请输入班级人数: students=");
}while(!int.TryParse(Console.ReadLine(),out acount_students));
//acount_students一定数字了


--------------------编程问答--------------------
引用 1 楼 trunjun 的回复:
兄弟,别纠结这种问题了。
我当年也犯过和你类似的问题。这个没必要非要限制。
你还是在继续学习下知识吧,这问题以后就不是问题了。
我就想多了解点。0.0 --------------------编程问答--------------------
引用 2 楼 thedolphin 的回复:
private void txtmoney_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 46 || e.KeyChar == 8)
            {
     ……
我看你写的好像不是控制台程序 --------------------编程问答-------------------- 你最好是别了解了。因为以后你根本不会用它做事情。

控制台界面对于大多数人来说就是用来学编程的——因为很简单,学到一定程度之后通常就不再用它了。 --------------------编程问答--------------------
引用 4 楼 wuyazhe 的回复:
C# code
int acount_students;
do
{
    Console.WriteLine("请输入班级人数: students=");
}while(!int.TryParse(Console.ReadLine(),out acount_students));
//acount_students一定数字了
这个貌似于  一个人要说话 你说:不要说了 他还在说 你继续说: 不要说了,一直没完没了 --------------------编程问答--------------------
引用 11 楼 zcasd123 的回复:
引用 4 楼 wuyazhe 的回复:

C# code
int acount_students;
do
{
Console.WriteLine("请输入班级人数: students=");
}while(!int.TryParse(Console.ReadLine(),out acount_students));
//acount_students一定数字了
这个貌似于  一……

没看到是循环啊,用正则就简单 --------------------编程问答-------------------- 我表示不再纠结!完贴 --------------------编程问答-------------------- 貌似来晚了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,