判断数字奇偶性 Asp.NetC#

1.输入是数字

专注于为中小企业提供网站设计、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业商丘免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

2.小数无奇偶性

3.奇偶性判断    n%2==0  偶数  反之为奇数

因为是初学Asp.Net,头文件using老是没写全。其实我还不知道那些要写,那些不用写上。  必须尽快学会

 

 

  1. using System; 
  2. using System.Text; 
  3. using System.Collections.Generic; 
  4. using System.Linq; 
  5. using System.Web; 
  6. using System.Web.UI; 
  7. using System.Web.UI.WebControls; 
  8.  
  9. namespace WebApplication2 
  10. { 
  11.     public partial class _Default : System.Web.UI.Page 
  12.     { 
  13.         protected void Page_Load(object sender, EventArgs e) 
  14.         { 
  15.             
  16.         } 
  17.  
  18.  
  19.   
  20.      
  21.         private static int IsNumeric(string str) //接收一个string类型的参数,保存到str里 
  22.         { 
  23.           char[] a=str.ToCharArray();; 
  24.            int  i; 
  25.             if (str == null || str.Length == 0)  
  26.                 //验证这个参数是否为空 
  27.                 return 0;                           //是,就返回False 
  28.             for(i=0;i
  29.             { 
  30.              
  31.                 if (a[i]=='.')                          //判断是否为 0.1 
  32.                 { 
  33.                     
  34.                     return 2;                     //不是,就返回False 
  35.                 } 
  36.                 
  37.             } 
  38.        
  39.              
  40.           
  41.             ASCIIEncoding ascii = new ASCIIEncoding();//new ASCIIEncoding 的实例 
  42.              byte[] bytestr = ascii.GetBytes(str);         //把string类型的参数保存到数组里 
  43.  
  44.              foreach (byte c in bytestr)                   //遍历这个数组里的内容 
  45.             { 
  46.                 if (c < 48 || c > 57)                          //判断是否为数字 
  47.                 { 
  48.                     return 0;                              //不是,就返回False 
  49.                 } 
  50.                  
  51.             } 
  52.             return 1;                                        //是,就返回True 
  53.         } 
  54.  
  55.        
  56.  
  57.         protected void TextBox1_TextChanged(object sender, EventArgs e) 
  58.         { 
  59.             int i = IsNumeric(TextBox1.Text); 
  60.             TextBox2.Text = i.ToString(); 
  61.             if (i == 1) 
  62.             { 
  63.                 int a = Int16.Parse(TextBox1.Text); 
  64.  
  65.                 if (a % 2 == 0) { TextBox2.Text = "偶数"; } 
  66.                 else if (a % 2 != 0) { TextBox2.Text = "奇数"; } 
  67.             } 
  68.             else if (i == 0) 
  69.                 TextBox2.Text = "输入错误!"; 
  70.             else if (i == 2) 
  71.                 TextBox2.Text = "小数无奇偶性"; 
  72.  
  73.             
  74.         } 
  75.  
  76.       
  77.     } 
  78. } 

网站栏目:判断数字奇偶性 Asp.NetC#
文章路径:http://www.hdfswf.com/article/pcojdp.html

其他资讯