溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

判斷數(shù)字奇偶性   Asp.Net C#

發(fā)布時間:2020-04-10 18:07:51 來源:網(wǎng)絡(luò) 閱讀:1500 作者:275821115 欄目:編程語言

1.輸入是數(shù)字

2.小數(shù)無奇偶性

3.奇偶性判斷    n%2==0  偶數(shù)  反之為奇數(shù)

因為是初學(xué)Asp.Net,頭文件using老是沒寫全。其實我還不知道那些要寫,那些不用寫上。  必須盡快學(xué)會

 

 

  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類型的參數(shù),保存到str里 
  21.         { 
  22.           char[] a=str.ToCharArray();; 
  23.            int  i; 
  24.             if (str == null || str.Length == 0)  
  25.                 //驗證這個參數(shù)是否為空 
  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類型的參數(shù)保存到數(shù)組里 
  42.  
  43.              foreach (byte c in bytestr)                   //遍歷這個數(shù)組里的內(nèi)容 
  44.             { 
  45.                 if (c < 48 || c > 57)                          //判斷是否為數(shù)字 
  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 = "偶數(shù)"; } 
  65.                 else if (a % 2 != 0) { TextBox2.Text = "奇數(shù)"; } 
  66.             } 
  67.             else if (i == 0) 
  68.                 TextBox2.Text = "輸入錯誤!"
  69.             else if (i == 2) 
  70.                 TextBox2.Text = "小數(shù)無奇偶性"
  71.  
  72.             
  73.         } 
  74.  
  75.       
  76.     } 
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI