判断数字奇偶性   Asp.Net C#

发布时间:2020-04-10 18:07:51 作者:275821115
来源:网络 阅读:1489

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

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

奇偶性 bs ne

上一篇:又说HRmis人力资源管理信息系统

下一篇:再和“面向对象”谈恋爱 - class(四)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》