C#_基礎題1-10套答案


one

 

1.用戶輸入一個整數,用 if ... else 判斷是偶數還是奇數
             Console.WriteLine( "請輸入一個整數:" );
             int  a = Convert.ToInt32(Console.ReadLine());
             if  (a / 2 == 0)
             {
                 Console.WriteLine( "偶數" );
             }
             else
             {
                 Console.WriteLine( "奇數" );
             }
 
2.輸入一個字母,判斷是大寫還是小寫字母
             Console.WriteLine( "請輸入一個字母:" );
             char  ch = char .Parse(Console.ReadLine());
             if  (ch > 'a'  && ch < 'z' )
             {
                 Console.WriteLine( "小寫" );
             }
             else
                 Console.WriteLine( "大寫" );
 
3.求1~99所有奇數的和,用 while 語句
             int  i = 0, sum = 0;
             while  (i<100)
             {
                 sum += i;
                 i++;
             }
             Console.WriteLine(sum);
 
4.用戶輸入三個整數,將最大數和最小數輸出
             Console.WriteLine( "請輸入第1個數:" );
             int  a = Convert.ToInt32(Console.ReadLine());
             Console.WriteLine( "請輸入第2個數:" );
             int  b = Convert.ToInt32(Console.ReadLine());
             Console.WriteLine( "請輸入第3個數:" );
             int  c = Convert.ToInt32(Console.ReadLine());
             int  max = Math.Max(Math.Max(a, b), c);
             int  min = Math.Min(Math.Min(a, b), c);
             Console.WriteLine( "max={0},min={1}" ,max,min);
      
5.輸入三個數,按從小到大的順序排列
   比較特殊的做法:
             int [] num = new  int [3];
             Console.WriteLine( "請輸入第1個數" );
             num[0] = Convert.ToInt32(Console.ReadLine());
             Console.WriteLine( "請輸入第2個數" );
             num[1] = Convert.ToInt32(Console.ReadLine());
             Console.WriteLine( "請輸入第3個數" );
             num[2]= Convert.ToInt32(Console.ReadLine());
             //int min = num[0] < num[1] ? (num[0] < num[2] ? num[0] : num[2]) : (num[1] < num[2] ? num[1] : num[2]);
             int  min =Math.Min(Math.Min(num[0],num[1]),num[2]);
             int  max = Math.Max(Math.Max(num[0],num[1]),num[2]);
             for  ( int  i = 0; i < 3; i++)
             {
                 if  (num[i]<max&&num[i]>min)
                 {
                     Console.WriteLine( "{0},{1},{2}" ,min,num[i],max);
                 }
             }
 
    一般的做法:
             Console.WriteLine( "請輸入第1個數:" );
             int  a = Convert.ToInt32(Console.ReadLine());
             Console.WriteLine( "請輸入第2個數:" );
             int  b = Convert.ToInt32(Console.ReadLine());
             Console.WriteLine( "請輸入第3個數:" );
             int  c = Convert.ToInt32(Console.ReadLine());
             int  temp;
             if  (a > b) { temp = a; a = b; b = temp; }
             if  (a > c) { temp = a; a = c; c = temp; }
             if  (b > c) { temp = b; b = c; c = temp; }
             Console.WriteLine( "{0},{1},{2}" ,a,b,c);
 
6.將1~200末位數為5的整數求和
             int  sum = 0;
             for  ( int  i = 1; i <= 200; i++)
             {
                 if  (i % 5==0)
                 {
                     sum += i;
                 }
             }    
             Console.WriteLine(sum);
 
7.計算2.5的3次方
            方法一:
             double  sum =1;
             for  ( int  i = 0; i < 3; i++)
             {
                sum *= 2.5;
             }
             Console.WriteLine(sum);
            方法二:           
            Console.WriteLine(Math.Pow(2.5, 3));
    
 
8.將24的所有因子求積
             int  sum = 0;
             for  ( int  i = 1; i <= 24; i++)
             {
                 if  (24%i==0)
                 {
                     sum += i;
                 }
             }
             Console.WriteLine(sum);
 
9.輸入一個年份看是否為閏年
                 int  i = Convert.ToInt32(Console.ReadLine());
                 if  (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
                 {
                     Console.WriteLine( "閏年" );
 
                 }
                 else
                     Console.WriteLine( "平年" );
 
 
10.輸入一段字符判斷是大寫,還是小寫。若是小寫,轉換為大寫,若是大寫,轉換為小寫
             string  a = Convert.ToString(Console.ReadLine());
             char  b = char .Parse(a);
             if  (b >= 'a'  && b <= 'z' )
             {
                 Console.WriteLine(a.ToUpper());
             }
             else
                 Console.WriteLine(a.ToLower());
 
 
11.判斷一個數是否為素數(質數)
             int  b = 1;
             int  a = Convert.ToInt32(Console.ReadLine());
             for  ( int  i = 2; i <= a/2; i++)
             {
                 if  (a % i == 0)
                 {
                     b = 0;
                     break ;
                 }
 
             }
             if  (b == 0)
             {
                 Console.WriteLine( "不是素數" );
             }
             else
                 Console.WriteLine( "是素數" );
 
12.一個名為Circle類包含一個字段(半徑),兩個方法,一個方法求面積,另一個方法求周長,並在Main函數中傳遞相應的數據,來顯示他的周長和面積
     class  Circle
     {
         public  double  sq( double  r)
         {
             return  Math.PI * r * r;
         }
         public  double  zc( double  r)
         {
             return  2 * Math.PI * r;
         }
     }
     class  Program
     {
         static  void  Main( string [] args)
         {
             Circle cir = new  Circle();
             Console.WriteLine( "sq={0},zc={1}" ,cir.sq(3),cir.zc(3));
         }
     }
 
13.設計一個形狀為figure,字段的個數和構造函數的設計自己考慮,另外設計一個虛方法
   設計一個圓的類Circle,繼承figure類,並重寫求面積的方法
   設計完成后在Main函數中定義square和circle的實例,並調用求面積的方法,輸出他們的面積
     class  Figure
     {
         public  virtual  double  sq( int  a, int  b)
         {
             return  a + b;
         }
         public  virtual  double  cq( double  r)
         {
             return  r;
         }
     }
     
     class  Circle:Figure
     {
         public  override  double   cq( double  r)
         {
             return  Math.PI * r * r;
         }
     }
     class  Square:Figure
     {
         public  override  double  sq( int  a, int  b)
         {
             return  a * b;
         }
     }
     class  Program
     {
         static  void  Main( string [] args)
         {
             Circle cir = new  Circle();
             Console.WriteLine( "cq={0}" ,cir.cq(3));
             Square sq = new  Square();
             Console.WriteLine( "sq={0}" , sq.sq(1,2));
         }
     }
 
14.設計一個類,包含兩個字段( double 類型)和一個構造函數,初始化這兩個字段,另外還包含一個虛方法(輸出這兩個字段的和)
構造另外一個類,此類繼承設計的那個類要求重寫虛方法(輸出這兩個數的積)
在Main函數中定義上面類的實例,傳入適當的參數,使得他們在屏幕上顯示。
     class  FatherClass
     {
         public  double  a;
         public  double  b;
         public  FatherClass()
         {
             this .a = a; this .b = b;
         }
         public  virtual  double  Sum( double  a, double  b)
         {
             return  a + b;
         }
     }
     class  SonClass:FatherClass
     {
         //public SonClass() : base() { }構造函數
         public  override  double   Sum( double  a, double  b)
         {
              return  a * b;
         }
     }
     class  Program
     {
         static  void  Main( string [] args)
         {
             FatherClass fs = new  FatherClass();
             SonClass ss = new  SonClass();
             Console.WriteLine(ss.Sum(2.2,3.3));
         }
     }

 

two

 

1.  輸入一個十進制的數,輸出它的十六進制數。
             Console.WriteLine( "請輸入一個十進制的數:" );
             int  i = int .Parse(Console.ReadLine());
             Console.WriteLine( "它的十六進制數是:{0:x}" , i);
 
 
2.  輸入一個英文小寫字母,將它轉換成大寫字母。
             //第一種方法:
             Console.WriteLine( "請輸入一個英文字母:" );
             char  a = Convert.ToChar(Console.ReadLine());
             if  (a > 'a'  && a < 'Z' )
             {
                 Console.WriteLine(a.ToString());
             }
             else
                 Console.WriteLine(a.ToString().ToUpper());
 
             // 第三種方法:(int類型的轉換)
             int  c = Convert.ToInt32(Console.Read());
             char  x = ( char )c;
             Console.WriteLine(x.ToString().ToUpper());
 
             //第二種方法(char類型)
             Console.WriteLine( "請輸入一個英文字母:" );
             char  a = Convert.ToChar(Console.Read());
             if  (a > 65 && a < 90)
             {
                 Console.WriteLine(a);
             }
             else
             {
                 Console.WriteLine(a.ToString().ToUpper());
             }
              
             //第四種方法:(string類型)
             Console.WriteLine( "請輸入一個英文小寫字母:" );
             string  a = Convert.ToString(Console.ReadLine());
             Console.Write(a.ToUpper());
 
 
 
3.  輸入三個整數,求三個數的和。
             Console.WriteLine( "請輸入三個整數:" );
             int  a = int .Parse(Console.ReadLine());
             int  b = int .Parse(Console.ReadLine());
             int  c = int .Parse(Console.ReadLine());
             
             //方法一:
             int  sum = a + b + c;
             Console.WriteLine( "這三個整數的和是:{0}" , sum);
 
             //方法二:
             Console.WriteLine( "這三個整數的和是:{0}" , a + b + c);
 
 
 
 
 
4.  輸入一個DOUBLE類型數,輸出它的整數部分。
             Console.WriteLine( "輸入一個DOUBLE類型數:" );
             double  a = Convert.ToDouble(Console.ReadLine());
             Console.WriteLine( "{0:#}" , a);
 
5.  輸入兩個數輸出其中最大的一個。(使用三目運算符)。
 
             Console.WriteLine( "請輸入兩個數:" );
             int  a = int .Parse(Console.ReadLine());
             int  b = int .Parse(Console.ReadLine());
             int  max;
             max = a > b ? a : b;
             Console.WriteLine( "max={0}" , max);
6.  求1+2+3+……+10的和。
 
             int  a;
             int  sum = 0;
             for  (a = 1; a <= 10; sum += a++) ;
             Console.WriteLine( "sum={0}" , sum);
 
7.  輸入一個圓的直徑,求它的周長和面積。
 
             Console.WriteLine( "請輸入一個圓的直徑:" );
             double  d = Convert.ToDouble(Console.ReadLine());
             double  c = 2 * Math.PI * d / 2;
             double  s = Math.PI * d / 2 * d / 2;
             Console.WriteLine( "周長={0},面積={1}" , c, s);
 
8.  輸入一個數,判斷它是否同時被3和5整除。
 
             Console.WriteLine( "請輸入一個數:" );
             int  s = int .Parse(Console.ReadLine());
             if  (s % 3 == 0 && s % 5 == 0)
             {
                 Console.WriteLine( "{0}能夠同時被3和5整除" , s);
             }
             else
                 Console.WriteLine( "{0}不能夠同時被3和5整除" , s);
9.  說出console.write()與console.writeline();console.read()與console.readline(); const readonly  ;%與/的區別。
             /* console.write()與console.writeline();
              * console.writeLine()換行,而console.write()不換行
              * console.read()與console.readline();
              * console.readline()是讀取輸入的一行的數據,而console.read()是讀取首字母的ASC的值
              * const readonly
              * const  編譯常量 ,是靜態的 ,不能和 static 一起用 ; readonly  執行常量
              * %與/的區別*
              * %是取余,/是取整
              * /
 
10. 打印出:   NAME    SEX     ADDRESS
              SA        16       BEIJING
          TT        20       WUHAN
 
 
             Console.WriteLine( "NAME\tSEX\tADDRESS" );
             Console.WriteLine( "SA\t16\tBEUING" );
         Console.WriteLine( "TT\t20\tWUHAN" );

 

three

 

 

 

four

 

1.  輸入一個郵箱地址,如果正確則顯示success否則顯示error(正確的郵箱地址包含@)。
Console.WriteLine( "請輸入一個郵箱地址:" );
             bool  b = true ;
             while  (b)
             {
                 string  str = Convert.ToString(Console.ReadLine());
                 if  (str.Contains( "@" ) && str.Contains( ".com" ))
                 {
                     Console.WriteLine( "Success" );
                 }
                 else
                     Console.WriteLine( "error" );
             }
2.  輸入一個小寫字母,要求輸出它的大寫字母。
Console.Write( "請輸入一個小寫字母:" );
             string  a = Convert.ToString(Console.ReadLine());
        Console.WriteLine(a.ToUpper());
       //字符串大小寫相互轉換
        //String類型
             while  ( true )
             {
                 string  str = Convert.ToString(Console.ReadLine());
                 char  ch = char .Parse(str);
                 if  (ch >= 'a'  && ch <= 'z' )
                 {
                     Console.WriteLine(str.ToUpper());
                 }
                 else
                     Console.WriteLine(str.ToLower());
             }
============================================================================================
        //Char類型
             while  ( true )
             {
                 char  ch = char .Parse(Console.ReadLine());
                 //if (ch >= 'a' && ch <= 'z')
                 if  (ch >= 97 && ch <= 122)
                 {
                     Console.WriteLine(( char )(ch - 32));
                 }
                 else
                     Console.WriteLine(( char )(ch + 32));
             }
 
3.  輸入一個5位數整數,將其倒向輸出。(例如:輸入12345,輸出 54321). 
             while  ( true )
             {
                 Console.WriteLine( "輸入整數:" );
                 int  a = Convert.ToInt32(Console.ReadLine());
                 int  length = a.ToString().Length;
 
                 int [] b = new  int [length];
 
                 for  ( int  i = 0; i <= length - 1; i++)
                 {
                         int  num = length - i;
                         int  num1 = Convert.ToInt32(Math.Pow(10, num));
                         int  num2 = Convert.ToInt32(Math.Pow(10, num - 1));
                         int  cout1 = a % num1;
                         b[i] = cout1 / num2; 
                         //Console.Write(b[num - 1]);
                 }
==================================================================
方法一:使用Array類的Reverse()方法
                 Array.Reverse(b);
                 foreach  ( int  m in  b)
                 {
                      Console.Write(m);
                 }
                 Console.Write( "\n" );  
             }
==================================================================
方法二:使用堆棧
                 Stack numbers = new  Stack();
                 //填充堆棧
                 foreach  ( int  number in  b)
                 {
                     numbers.Push(number);
                     //Console.Write(number);
                 }
                 //遍歷堆棧
                 foreach  ( int  number in  numbers)
                 {
                     //Console.Write(number);
                 }
                 //清空堆棧
                 while  (numbers.Count != 0)
                 {
                     int  number = ( int )numbers.Pop();
                     Console.Write(number);
                 }
                 Console.Write( "\n" );
 
==================================================================
 
輸入幾個數按照1,2,3的格式輸出(簡單的說就是每兩個數字之間必須用逗號隔開)
             string  str = Convert.ToString(Console.ReadLine());
             string [] arraystr = str.Split( ',' ); //數組的動態存儲,用Realdine讀取。
             Array.Reverse(arraystr); //逆着輸出
             foreach  ( string  i in  arraystr)
             {
                 Console.Write( "{0}\0" ,i);
             }
4.  從界面輸入5個數, 然后根據這5個數,輸出指定數目的”*”,如有一個數為5,則輸出”*****”.
             while  ( true )
             {
                 int  n = int .Parse(Console.ReadLine());
                 for  ( int  i = 0; i < n; i++)
                 {
                     Console.Write( "*" );
                 }
                 Console.Write( "\n" );
             }
 
5.  將如下字符串以”.”分割成3個子字符串. ” www.softeem.com”。
            string  a = "www.softeem.com" ;
             string [] str = a.Split( '.' );
             Console.WriteLine( "{0}\0{1}\0{2}" ,str[0],str[1],str[2]);
 
算法:不管輸入多長如例子a.b.c.e.f.g.h.i 輸出為 a b c d e f g h有點則分割,沒點就輸出
             bool  b = true ;
             while  (b)
             {
                 Console.Write( "輸入數據:" );
                 string  a = Convert.ToString(Console.ReadLine());
                 if  (a.Contains( "." ))
                 {
                     Console.WriteLine(a.Replace( '.' , ' ' )); //此處用到了替換,直接將‘.’替換為 '';
                 }
                 else
                     Console.WriteLine(a);
             }
6.  1 2 3 5          3 5 7 9
4 5 6 2          4 0 3 0
5 6 2 3    +     2 3 0 8        =?
8 5 3 1          9 7 5 1
int [,] num1 = { { 1, 2, 3, 5 }, { 4, 5, 6, 2 }, { 5, 6, 2, 3 }, { 8, 5, 3, 1 } };
             int [,] num2 = { { 3, 5, 7, 9 }, { 4, 0, 3, 0 }, { 2, 3, 0, 8 }, { 9, 7, 5, 1 } };
             int [,] sum = new  int [4, 4];
             for  ( int  i = 0; i < 4; i++)
             {
                 for  ( int  j = 0; j < 4; j++)
                 {
                     sum[i, j] = num1[i, j] + num2[i, j];
                 }
             }
             for  ( int  i = 0; i < 4; i++)
             {
                 for  ( int  j = 0; j < 4; j++)
                 {
                     Console.Write( "{0}\t" , sum[i, j]);
                 }
                 Console.Write( "\n" );
             }
          }
7.    2   4   9            1  2
       1   3   5     *      4  5          =?
                            1  0                               
           int [,] num1 = new  int [2, 3] { { 2, 4, 9 }, { 1, 3, 5 } };
             int [,] num2 = new  int [3, 2] { { 1, 2 }, { 4, 5 }, { 1, 0 } };
             int [,] sum = new  int [2, 3];
             for  ( int  i = 0; i < 2; i++)
             {
                 for  ( int  j = 0; j < 3; j++)
                 {
                     sum[i, j] = num1[i, j] * num2[j, i];
                 }
             }
             for  ( int  i = 0; i < 2; i++)
             {
                 for  ( int  j = 0; j < 3; j++)
                 {
                     Console.Write( "{0}\t" , sum[i, j]);
                 }
                 Console.Write( "\n" );
             }
          }
 
8   輸入a,b,c的值。求  ax*x+bx+c=0的根。
             while  ( true )
             {
                 Console.WriteLine( "請輸入三個數:" );
                 double  a = Convert.ToInt32(Console.ReadLine());
                 double  b = Convert.ToInt32(Console.ReadLine());
                 double  c = Convert.ToInt32(Console.ReadLine());
                 double  d = b * b - 4 * a * c;
                 double  x1 = (Math.Sqrt(d) - b) / (2 * a);
                 double  x2 = (-b - (Math.Sqrt(d))) / (2 * a);
                 if  (b * b - 4 * a * c < 0)
                 {
                     Console.WriteLine( "該方程沒有根" );
                 }
                 else  if  (x1==x2)
                 {
                     Console.WriteLine( "該方程的根是:x1=x2={0}" , x1);
                 }
                 else
                     Console.WriteLine( "該方程的根是:x1={0},x2={1}" , x1, x2);
             }
9   輸入5個數,將其重小到大排列輸出。(使用冒泡)
             非冒泡排序
Console.WriteLine( "請輸入五個數" );
             int  a = int .Parse(Console.ReadLine());
             int  b = int .Parse(Console.ReadLine());
             int  c = int .Parse(Console.ReadLine());
             int  d = int .Parse(Console.ReadLine());
             int  e = int .Parse(Console.ReadLine());
             int  min1 = a < b ? (a < c ? a : c) : (b < c ? b : c); //比較三個數的大小
             int  min2 = d < e ? d : e;
             int  min = min1 < min2 ? min1 : min2;
             Console.WriteLine(min);
             
             冒泡排序:
             Console.Write( "請按1,2,3的格式輸入幾個數字:" );
             string  str = Convert.ToString(Console.ReadLine());
             string [] arraystr = str.Split( ',' );
             int  arraylength = arraystr.Length;
             int [] num = new  int [arraylength];
             int  count = arraylength - 1;
             for  ( int  i = 0; i <arraylength; i++)
             {
                 num[i] = int .Parse(arraystr[i]);
             }
             for  ( int  i = 0; i < count; i++)
             {
                 for  ( int  j = 0; j < count - i; j++)
                 {
                     if  (num[j] > num[j + 1])
                     {
                         int  temp;
                         temp = num[j];
                         num[j] = num[j + 1];
                         num[j + 1] = temp;
                     }
                 }
             }
             foreach  ( int  i in  num)
             {
                 Console.Write( "{0}\0" ,i);
             }
 
 
10  輸入一個數n,求n!。(使用遞歸)
bool  b = true ;
             while  (b)
             {
                 Console.Write( "輸入數:" );
                 int  n = Convert.ToInt32(Console.ReadLine());
                 int  sum = 1;
                 for  ( int  i = 1; i <= n; i++)
                 {
                     sum *= i;
                 }
                 Console.WriteLine(sum);
             }
}

 

five

 

1.  創建一個Employ類, 實現一個計算全年工資的方法.
     class  Employ
     {
         public  int  Getyearmoney( int  monthmoney)
         {
             return  12 * monthmoney;
         }
         static  void  Main( string [] args)
         {
             Employ employ = new  Employ();
             Console.Write( "請輸入月工資:" );
             int  monthmoney = Convert.ToInt32(Console.ReadLine());
             Console.WriteLine( "年工資為:{0}" ,employ.Getyearmoney(monthmoney));
         }
     }
 
         
2.  定義一個方法, 既可以求2個整數的積,也可以求3或4個整數的積.
         public  static  int  Cj( params  int [] nums)
         {
             int  Mul = 1;
             foreach  ( int  num in  nums)
             {
                 Mul *= num;
             }
             return  Mul;
         }
         static  void  Main( string [] args)
         {
             ///將數字按照字符串的格式輸出
             Console.WriteLine( "請輸入數據:" );
             string  a = Convert.ToString(Console.ReadLine());
             string [] str = a.Split( ',' );
             int [] num = new  int [str.Length];
             for  ( int  i = 0; i < str.Length; i++)
             {
                 num[i]= int .Parse(str[i]); //將string[]轉換為int[]類型;
             }
             int  c = 0;
             foreach  ( int  i in  num) //遍歷數組乘積累積
             {
                 c = Cj(num);
             }
             Console.WriteLine(c);
         }
 
 
 
3.  編寫一個方法,求兩點(x1,y1)與(x2,y2)之間的距離(用戶自己輸入點的坐標);
     class  Program
     {
         public  int  Mul( int  x1, int  y1, int  x2, int  y2)
         {
             return  Convert.ToInt32(Math.Sqrt(Convert.ToDouble((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
         }
         static  void  Main( string [] args)
         {
             Program pr = new  Program();
             Console.Write( "第一個點的橫坐標:" );
             int  x1 =Convert.ToInt32(Console.ReadLine());
             Console.Write( "第一個點的縱坐標:" );
             int  y1 =Convert.ToInt32(Console.ReadLine());
             Console.Write( "第二個點的橫坐標:" );
             int  x2 =Convert.ToInt32(Console.ReadLine());
             Console.Write( "第二個點的縱坐標:" );
             int  y2 =Convert.ToInt32(Console.ReadLine());
             Console.WriteLine( "這兩個點之間的距離是:{0}" ,pr.Mul(x1, y1, x2, y2));
         }
      }
 
 
     class  Program
     {
         /// <summary>
         /// 求兩點間的距離
         /// </summary>
         /// <param name="point"></param>
         /// <returns></returns>
         public  int  Fartwopoint( int [,] point)
         {
             Console.Write( "第一個點的橫坐標:" );
             int  x1 = int .Parse(Console.ReadLine());
             Console.Write( "第一個點的縱坐標:" );
             int  y1 = int .Parse(Console.ReadLine());
             Console.Write( "第二個點的橫坐標:" );
             int  x2 = int .Parse(Console.ReadLine());
             Console.Write( "第二個點的縱坐標:" );
             int  y2 = int .Parse(Console.ReadLine());
             int [,] a = new  int [2, 2] { { x1, y1 }, { x2, y2 } };
             double  d = Convert.ToDouble((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
             int  c = Convert.ToInt32(Math.Sqrt(d));
             return  c;
         }
         static  void  Main( string [] args)
         {
             Program pr = new  Program();
             int [,] point = new  int [2, 2];
             Console.WriteLine(pr.Fartwopoint(point));
         }
     }
方法二:
     class  Program
     {
         public  int  Mul( int  x1, int  y1, int  x2, int  y2)
         {
             return  Convert.ToInt32(Math.Sqrt(Convert.ToDouble((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
         }
         static  void  Main( string [] args)
         {
             Program pr = new  Program();
             Console.Write( "第一個點的橫坐標:" );
             int  x1 =Convert.ToInt32(Console.ReadLine());
             Console.Write( "第一個點的縱坐標:" );
             int  y1 =Convert.ToInt32(Console.ReadLine());
             Console.Write( "第二個點的橫坐標:" );
             int  x2 =Convert.ToInt32(Console.ReadLine());
             Console.Write( "第二個點的縱坐標:" );
             int  y2 =Convert.ToInt32(Console.ReadLine());
             Console.WriteLine( "這兩個點之間的距離是:{0}" ,pr.Mul(x1, y1, x2, y2));
         }
      }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     方法三:
     class  Point
     {
         private  int  x1;
         private  int  y1;
         private  int  x2;
         private  int  y2;
         public  int  Jl( int  x1, int  y1, int  x2, int  y2)
         {
             int  z = Convert.ToInt32(Math.Sqrt(Convert.ToInt32((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
             return  z;
         }
 
         static  void  Main( string [] args)
         {
             Point poi = new  Point();
             Console.WriteLine(poi.Jl(poi.x1,poi.x2,poi.y1,poi.y2));
          }       
    }
4.  創建一個Student類, 定義5個字段和2個方法,上述類里, 定義兩個同名方法GetAge, 一個返回年齡,一個根據傳入的出生年月,返回年齡。
     class  Student
     {
         private  int  id;
         private  int  age;
         private  string  name;
         private  string  sex;
         private  DateTime birthday;
 
         public  int  GetAge( int  age)
         {
             return  age;
         }
         public  int  GetAge(DateTime birthday)
         {
             return  DateTime.Now.Year - birthday.Year + 1;
 
         }
         static  void  Main( string [] args)
         {
             Console.WriteLine( "請輸入出生日期:" );
             string  stubirthday = Convert.ToString(Console.ReadLine());
             Student stu = new  Student();
             stu.birthday = Convert.ToDateTime(stubirthday);
             Console.WriteLine( "年齡:{0}" ,stu.GetAge(stu.birthday));
         }
 
     }
 
5.  編寫一個方法,輸入一個整數,然后把整數一反序返回(比如輸入8976,返回6798);
8976 = 8 x 1000 +9 x 100 +7 x 10 +6 x 1;
6798 = 6 x 1000 +7 x 100 +9 x10 +8 x 1;
1000 = Math.Pow (10, a.Length); a-length --;
6 = a %10 ,7 = a /1000 - a/100,9 = a/100 -a/10
6.  定義一個類,完成一個方法,以數字表示的金額轉換為以中文表示的金額, 如下24000,098.23貳千肆佰萬零玖拾捌元貳角八分,1908.20 壹千玖佰零捌元貳角
 
 
7.  static  ,viod 是什么意思, get set ref out public private 的區別是什么。
Static靜態類
Void 無返回值
get  可讀, set  可寫
ref 開始必須賦值
out 開始不須直接賦值
public  公共可訪問的類
private 私有的僅在內部類中使用

 

six

 

1.  封裝一個N!,當輸入一個數時,輸出它的階層。
 
     sealed  class  Data //密封類
     {
         public  int  N( int  a)
         {
             int  sum =1;
             for ( int  i =1;i<=a;i++)
             {
                 sum *= i;
             }
             return  sum;
         }
}
 
    private  void  button1_Click( object  sender, EventArgs e)
    {
         Data dt = new  Data();
         MessageBox.Show(dt.N(3).ToString());
}
 
 
2.  定義一個索引,訪問其相關的內容。
==定義Student 和Students 兩個類
     public  class  Student
     {
         public  Student( int  id, string  name, int  age, string  sex)
         {
             this .id = id;
             this .name = name;
             this .age = age;
             this .sex = sex;
         }
         private  int  id;
 
         public  int  Id
         {
             get  { return  id; }
             set  { id = value; }
         }
         private  string  name;
 
         public  string  Name
         {
             get  { return  name; }
             set  { name = value; }
         }
         private  int  age;
 
         public  int  Age
         {
             get  { return  age; }
             set  { age = value; }
         }
         private  string  sex;
 
         public  string  Sex
         {
             get  { return  sex; }
             set  { sex = value; }
         }
     }
     public  class  Students
     {
         Student[] stu = new  Student[3];
         public  Students()
         {
             Student stu1 = new  Student(1, "a" ,23, "men" );
             Student stu2 = new  Student (2, "b" ,23, "women" );
             Student stu3 = new  Student (3, "c" ,21, "men" );
             stu[0]=stu1;
             stu[1]=stu2;
             stu[2]= stu3;
         }
         public  Student this [ int  index]
         {
             get
             {
                 return  stu[index];
             }
             set
             {
                 stu[index] =value;
             }
         }
}
 
         private  void  button1_Click( object  sender, EventArgs e)
         {
             Students stu = new  Students();
             MessageBox.Show(stu[0].Name+ " " +stu[1].Sex+ " " +stu[2].Id);
     }
 
3.  定義一個抽象類,並在子類中實現抽象的方法.
 
//定義抽象類Abtr
     abstract  class  Abtr
     {
         public  abstract  int  Num( int  a, int  b, int  c); // 抽象類的抽象方法
 
         public  virtual  int  Max( int  a, int  b, int  c) //抽象類的虛方法
         {
             int  max1 = Math.Max(a, b);
             int  max = Math.Max(max1, c);
             return  max;
         }
}
 
     //子類是抽象類
     abstract  class  CAbtr : Abtr
     {
         public  abstract  override  int  Num( int  a, int  b, int  c);
     }
 
     //子類不是抽象類時候
     class  CAbtr : Abtr
     {
         public  override  int  Num( int  a, int  b, int  c)
         {
             return  Convert.ToInt32(Math.Sqrt(Convert.ToDouble(a * b * c)));
         }
         public  override  int  Max( int  a, int  b, int  c)
         {
             int  max1 = Math.Max(a, b);
             int  max = Math.Max(max1, c);
             return  100*max;
         }
 
 
4.  有三角形,矩形,正方形,梯形,圓,橢圓,分別求出各自的周長,面積( abstract ).
 
     abstract  class  Refent
     {
         public  abstract  int  Sanjiaozc( int  d, int  h, int  a, int  b);
         public  abstract  int  Sanjiaomj( int  d, int  h, int  a, int  b);
         public  abstract  int  Juxizc( int  l, int  h);
         public  abstract  int  Juximj( int  l, int  h);
         public  abstract  int  Zhengzc( int  a);
         public  abstract  int  Zhengmj( int  a);
         public  abstract  int  Tixingzc( int  a, int  b , int  h, int  c, int  d);
         public  abstract  int  Tixingmj( int  a, int  b, int  h, int  c, int  d);
         public  abstract  int  Cirzc( int  r);
         public  abstract  int  Cirmj( int  r);
}
 
     class  CRent:Refent
     {
         int  zc; int  mj;
         public  override  int  Sanjiaozc( int  d, int  h, int  a, int  b)
         {
             zc = d+ a + b;
             return  zc;
         }
         public  override  int  Sanjiaomj( int  d, int  h, int  a, int  b)
         {
             mj = d * h / 2;
             return  mj;
         }
         public  override  int  Juxizc( int  l, int  h)
         {
             zc = 2*(l + h);
             return  zc;
         }
         public  override  int  Juximj( int  l, int  h)
         {
             mj = 2 * l * h;
             return  mj;
         }
 
         public  override  int  Zhengzc( int  a)
         {
             zc = 4 * a;
             return  zc;
         }
         public  override  int  Zhengmj( int  a)
         {
             mj = a * a;
             return  mj;
         }
         public  override  int   Tixingzc( int  a, int  b, int  h, int  c, int  d)
         {
             zc = a + b +c +d;
             return  mj;
         }
         public  override  int  Tixingmj( int  a, int  b, int  h, int  c, int  d)
         {
             mj = (a + b) * h / 2;
             return  mj;
         }
         public  override  int  Cirmj( int  r)
         {
             mj = Convert.ToInt32(Math.PI * r * r);
             return  mj;
         }
         public  override  int  Cirzc( int  r)
         {
             zc = Convert.ToInt32(2 * Math.PI * r);
             return  zc;
         }
}
 
 
5.  分析:為什么String類不能被繼承.
string 類是密封類
 
 
6.  定義一個接口,接口必須含有一個方法, 用一個實現類去實現他.
     interface  Inte
     {
         int  GetAge( string  date);
}
 
public  class  Employee:Inte
     {
         public  int  GetAge( string  date)
         {
             DateTime dt = DateTime.Parse(date);
             int  age =dt.Year - DateTime.Now.Year;
             return  age;
         }
     }
 
7.  說明接口和抽象類的區別.
1.  接口不能有構造函數,抽象類有構造函數
2.  接口允許方法的實現,抽象方法是不能被實現的
3.  接口里面不須 override 來重寫,抽象類一定要 override 重寫
 
 
8.  如果從TextBox輸入一個參數, 就求圓的周長, 兩個參數就求矩形的周長,三個參數就求三角形的周長.
***對於 params 的用法還不是很了解?ArrayList, 還有HashTable
     class  ZC
     {
         public  int  C( params  int [] a)
         {
             int  sum = 0;
             foreach  ( int  i in  a)
             {
                 if  (a.Length == 1)
                 {
                     sum += Convert.ToInt32(2 * Math.PI * i);
                 }
                 if  (a.Length == 2)
                 {
                     sum += 2* i;
                 }
                 if  (a.Length==3)
                 {
                     sum += i;
                 }
             }
             return  sum;
         }
     }
 
WinFrom中的代碼:
         private  void  button1_Click( object  sender, EventArgs e)
         {
             ZC zc = new  ZC();
             string  s = textBox1.Text.Trim();
             string [] str = s.Split( ',' );
             int [] a = new  int [str.Length];
             for  ( int  i = 0; i < str.Length; i++)
             {
                 a[i] = int .Parse(str[i]);
             }
             MessageBox.Show(zc.C(a).ToString());
         }
 
 
9.  說明重載與重寫的區別。
 
1.重載函數之間的參數類型或個數是不一樣的.重載方法要求在同一個類中.
(解釋:例如原來有一個構造函數
public  Student(){},
現在有重載了這個構造函數
public  Student( int  age , int  id, string  name)
{
    this .age = age ;
    this .name = name ;
    this .id = id;
})
 
2.重寫要求返回類型,函數名,參數都是一樣的,重寫的方法在不同類中.
 
10. 完成一個小型的計算器,不熟悉的功能可以不完成
11. 
12. Public private  internal  protected 的使用范圍。
  大到小:Public protected  internal  private
   Public 所有類, protected  子父類訪問 internal 當前項目, private 當前類

 

seven

 

1.定義一個委托,委托一個去借東西的方法Send,如果借到了返回GOOD!,否則NO!
class  Borrow
     {
         public  delegate  string  Send();
         public  string  Br(Send send)
         {
             return  send();
         }
         public  string  OK()
         {
             return  "GOOD!" ;
         }
         public  string  NO()
         {
             return  "NO!" ;
         }
}
調用:
Borrow b = new  Borrow();
             MessageBox.Show(b.Br(b.OK));
 
2.定義一個事件,當我激發它時,返回一個結果給我.
class  Event1
         {
             public  delegate  string  Eve();
             public  event  Eve eve;    //事件
             public  string  Trigger()
             {
                 if  (eve != null )
                     return  eve();
                 else
                     return  "" ;
             }
             public  string  A1()
             {
                 return  "A1" ;
             }
             public  string  A2()
             {
                 return  "A2" ;
             }
         }
調用:
Event1 event1 = new  Event1();
             event1.eve += event1.A1;
             MessageBox.Show(event1.Trigger());
 
3.定義一個拿100元錢去買東西的委托,書的價格是58元,本的價格是20元,返回剩余的錢.
class  Event1
         {
             public  delegate  int  Eve( int  sum);
             public  event  Eve eve;    //事件
             public  int  Trigger( int  sum)
             {
                 if  (eve != null )
                     return  eve(sum);
                 else
                     return  0;
             }
             public  int  Book( int  sum)
             {
                 return  sum-=58;
             }
             public  int  Text( int  sum)
             {
                 return  sum-=20;
             }
         }
調用:
Event1 event1 = new  Event1();
             int  sum = 100;
             event1.eve += event1.Book;
             sum = event1.Trigger(sum);
             event1.eve += event1.Text;
             sum = event1.Trigger(sum);
             MessageBox.Show(sum.ToString());
 
4.說出委托與事件的區別.
事件屬於委托,委托不屬於事件.事件是委托的一個變量.

 

 

eight

 

private  void  button1_Click( object  sender, EventArgs e)
{
     MessageBox.Show(Max(1, 4, 6, 8, 3).ToString());
}
public  int  Max( params  int [] array)
{
     int  max = Int32.MinValue;
     foreach  ( int  i in  array)
     {
         max = Math.Max(i, max);
     }
     return  max;
}
 
private  void  button2_Click( object  sender, EventArgs e)
{
     string  str = "" ;
     for  ( int  i = 0; i < 4; i++)
     {
         for  ( int  j = 0; j < 2*i+1; j++)
         {
             str += "* " ;
         }
         str += "\r\n" ;
     }
 
     for  ( int  i = 0; i < 3; i++)
     {
         for  ( int  j = 5; j > 2*i; j--)
         {
             str += "* " ;
         }
         str += "\r\n" ;
     }
     richTextBox1.Text = str;
}
 
private  void  button3_Click( object  sender, EventArgs e)
{
     DateTime date1 = DateTime.Parse( "1998-1-1 00:00:00" );
     DateTime date2 = DateTime.Parse( "2009-3-13 00:00:00" );
     TimeSpan time = date2 - date1;
     int  days = time.Days+1;
     if  (days % 5 == 0)
     {
         MessageBox.Show( "曬網" );
     }
     else
     {
         if  (days % 5 > 3)
         {
             MessageBox.Show( "曬網" );
         }
         else
         {
             MessageBox.Show( "打魚" );
         }
     }
}
 
/// <summary>
/// 求200小孩。。。。。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private  void  button4_Click( object  sender, EventArgs e)
{
     int  length = 200;
     int [] array = new  int [length];
     for  ( int  i = 0; i < length; i++)
     {
         array[i] = i + 1;
     }
     string  str = null ;
 
     for  ( int  i = 1; i <= length; i++)
     {
         str += i.ToString() + "," ;
     }
     Out(str, array);
     
}
public  void  Out( string  str, int  []array)
{
     if  (array.Length == 3)
     {
         MessageBox.Show(array[1].ToString());
     }
     else
     {
         int  lit = array.Length % 4;
         string  st = "" ;
         for  ( int  i = lit; i>0; i--)
         {
             st += array[array.Length - i].ToString()+ "," ;
         }
         
         for  ( int  i = 1; i <= array.Length; i++)
         {
             if  (i % 4 == 0)
             {
                 str=str.Replace( "," +array[i - 1].ToString() + "," , "," );
             }
         }
         int  length = str.Length;
         if  (st == "" )
         {
             str = str;
         }
         else
         {
             str = str.Replace(st, "" );
             str = st + str;
         }
       
         string [] arraystring = str.Split( ',' );
         int [] arrayint = new  int [arraystring.Length-1];
         for  ( int  i = 0; i <arraystring.Length-1; i++)
         {
             arrayint[i] = int .Parse(arraystring[i]);
         }
         Out(str, arrayint);
     }
 
}
 
private  void  button5_Click( object  sender, EventArgs e)
{
     int  sum = 0;
     Random rand = new  Random();
     for  ( int  i = 0; i < 100; i++)
     {
         
         int  x = rand.Next(65, 91);
         char  y = Convert.ToChar(x);
         if  (y == 'A'  || y == 'O'  || y == 'E'  || y == 'I'  || y == 'U' )
         {
             sum++;
         }
     }
     MessageBox.Show(sum.ToString());
 
}
 
private  void  button6_Click( object  sender, EventArgs e)
{
     string  str = "2," ;
     for  ( int  i = 3; i <= 100; i++)
     {
         int  j;
         for  ( j = 2; j < i; j++)
         {
             if  (i % j == 0)
             {
                 break ;
 
             }
           
         }
         if  (i==j)
         {
             str += i.ToString()+ "," ;
             
         }
     }
     MessageBox.Show(str);
}
 
private  void  button7_Click( object  sender, EventArgs e)
{
     int  i = 27863654;
     string  str = Convert.ToString(i, 2);
     string  sumstr = str.Replace( "0" , "" );
     MessageBox.Show(sumstr.Length.ToString());
}
 
private  void  button8_Click( object  sender, EventArgs e)
{
     string  str = "" ;
     for  ( int  i = 100; i < 1000; i++)
     {
         int  a = i % 10; //個位
         int  b = (i / 10) % 10; //十位
         int  c = i / 100; //百位
         int  sum = a * a * a + b * b * b + c * c * c;
         if  (sum == i)
         {
             str += i.ToString() + "," ;
         }
     }
     MessageBox.Show(str);
}
 
private  void  button9_Click( object  sender, EventArgs e)
{
     int  sum = 0;
     string  str = "" ;
     for  ( int  i = 2; i < 1000; i++)
     {
         for  ( int  j = 1; j < i; j++)
         {
             if  (i % j == 0)
             {
                 sum += j;
             }
         }
         if  (sum == i)
         {
             str += i.ToString() + "," ;
         }
         sum = 0;
         
     }
     MessageBox.Show(str);
}
 
private  void  button10_Click( object  sender, EventArgs e)
{
     int  sum = 0;
     int  length;
     for  ( int  i = 1; i < 9; i++)
     {
 
         sum += Convert.ToInt32( 200 * Math.Pow(2, i));
     }
     sum += 200;
 
     length = 100 + Convert.ToInt32(sum / Math.Pow(2, 9));
     MessageBox.Show(length.ToString());
}
 
private  void  button11_Click( object  sender, EventArgs e)
{
     int  n = 225;
     int  i = 2;
     string  str = "" ;
     while  (n != 1)
     {
         while  (n % i == 0)
         {
             if  (n / i !=1)
             {
                 str += i.ToString()+ "*" ;
             }
             else
             {
                 str += i.ToString();
             }
             
             n = n / i;
         }
         i++;
     }
     MessageBox.Show(str);
}
 
private  void  button12_Click( object  sender, EventArgs e)
{
     int  max = 20;
     int  min = 5;
     //最大公約數
     string  strmax = "" ;
     //for (int i = min; i >0; i--)
     //{
     //    if (max % i == 0 && min % i == 0)
     //    {
     //        strmax = i.ToString();
     //        break;
     //    }
     //}
     //zuixiaogongbeishu
     for  ( int  i = max; i <= max*min; i++)
     {
         if  (i%max==0 && i%min==0)
         {
             strmax += i.ToString() + "," ;
             break ;
         }
     }
     MessageBox.Show(strmax);
}
 
private  void  button13_Click( object  sender, EventArgs e)
{
     int [,] array = new  int [20, 2];
     array[0, 0] = 2;
     array[0, 1] = 1;
 
     for  ( int  i = 1; i < 20; i++)
     {
         for  ( int  j = 0; j < 2; j++)
         {
             if  (j == 0)
             {
                 array[i, j] = array[i - 1, j] + array[i - 1, j + 1];
             }
             else
             {
                 array[i, j] = array[i - 1, j - 1];
             }
         }
     }
 
     double  dmin = 1;
     for  ( int  i = 0; i < 20; i++)
     {
         dmin *= array[i, 1];
     }
     double  bsum = 0;
     for  ( int  i = 0; i < 20; i++)
     {
         bsum += (dmin / array[i, 1]) * array[i, 0];
     }
 
     MessageBox.Show(bsum.ToString()+ "/" +dmin.ToString());
     //double min=Math.Min(dmin,bsum);
     //double maxcount = 0;
     //for (double i = min; i > 0; i--)
     //{
     //    if (dmin % i == 0 && bsum % i == 0)
     //    {
     //        maxcount = i;
     //        break;
     //    }
     //}
     //string str = (bsum / maxcount).ToString() + "//" + (dmin / maxcount).ToString();
     //MessageBox.Show(str);
 
}
 
private  void  button14_Click( object  sender, EventArgs e)
{
     Humen student1 = new  Humen();
     student1.Age = 10;
     Humen student2 = new  Humen();
     student2.Age = student1.Age + 2;
}

 

 

nine

C#基礎測試題

一. 填空

1.      CLR中文意思是 公共語言運行庫      

2.      MSIL中文意思是  微軟中間語言           

3.      GC中文意思是    垃圾回收機制       

4.       Visual studio 2005 中INT 占   4     個字節.

5.      語句結構分為     順序結構,選擇結構,循環                                     結構.

6.      聲明一個空間用      namespace    關鍵字,聲明一個類用     class 關鍵字,定義抽象類用     abstract     關鍵字,定義密封類使用     sealed           關鍵字, 實例化一個對象使用                  new               是        關鍵字.類的默認修飾符是   internal     

7.      修飾符有  4   種,分別是  public protected internal private                                            

8.      LONG轉換成INT 屬於    顯示   轉換, INT 轉換成STRING 用            方法.STRING 轉換成INT 用       int.Parse()         方法,還可以使用                                     方法    Convert.ToInt32()          .

9.      彈出一個對話框使用     MessageBox.Show();            

10.  面向對象的三大特性是     繼承        ,     封裝        ,   多態         

11.  取出字符串中的子字符串用      Substring()          方法,替換用    Replace()         方法,分割用    Split()        方法,查找其中一項的位置用      IndexOf()          方法,查找其中是否包含用      Contains()          方法.

12.  快速排序用         Array.Sort()            ,倒序用           Array.Reverse()                 .

二. 選擇題

1.c#程序的主方法是(B )

A.main()            B.Main()              C.class()        D.namespace()

 

2.Console類位於下列哪個命名空間( C)

A.System.Text   B.System.IO        C.System      D.System.Collections

 

3.下列屬於值類型的有(B,C,D)(多選)

A.class        B.enum         C.struct         D.int             E.string

 

4. static const int i = 1   這句代碼是否有誤(A)

A.有  B.無

 

5.下列哪些方法能對字符串進行操作(ABCD)(多選)

A.Trim()             B.IndexOf()         C.Insert()             D.ToUpper()        E.Add()ArrayList HashTable

 

6.以下說法正確的是(AB)(多選)

A.構造函數名必須和類名相同

B.一個類可以聲明多個構造函數

C.構造函數可以有返回值

D.編譯器可以提供一個默認的帶一個參數的構造函數

 

7.下列關於抽象類的說法正確的是(ACD)(多選)

A.抽象類能夠擁有非抽象方法

B.抽象類可以被實例化

C.抽象類里可以聲明屬性

D.抽象類里可以聲明字段

8. 可用作C#程序用戶標識符的一組標識符是(B)

A. void    define    +WORD       B. a3_b3    _123     YN

C. for      -abc      Case         D. 2a      DO     sizeof

 

9. C#的數據類型有(B) 
A 值類型和調用類型 
B 值類型和引用類型 
C 引用類型和關系類型 
D 關系類型和調用類型

 

三. 判斷題

(1).一個子類可以有多個父類。錯

(2).子類可以直接繼承父類中的構造函數。錯

(3).抽象類里面可以有方法的實現。對

(4).int i,char x=‘a’,如果i=x,則i=97 對

(5).電腦是一個對象 錯

(6).String.Format(“{0}*{1}={2}”,2,3,4),打印結果為2*3=4 對

(7).抽象方法在子類必須實現  錯

(8).public int Add(Params int[]a)可以把一個數組當參數傳遞。對

(9).抽象類沒有構造函數 錯 有,不能被實例化

(10).string是密封類 對

(11).接口內有構造函數 錯

四. 分析題

1.寫出結果

 

 

2.寫出結果

 

 

2

5

1

6

 

 

五. 比較題

1. Console.Write();與Console.WriteLine();

 Console.Write()是輸出一行數據不換行

Cosole.WriteLine()是輸出一行數據換行

2.  Console.Read();與Console.ReadLine();

Console.Read()是讀出首字母的ASCII碼,后者是讀出字符串.

Console.ReadLine()是讀取一行數據

3.  const與readonly

const是靜態的編譯常量,不能和static在一起用

readonly是執行常量

4.  值類型和引用類型

(1) 值類型:以棧進行存儲.

(2) 引用類型:以堆進行存儲.

5. String與string,StringBuilder

String是C#中獨特數據類型 

string 是靜態類型

StringBuilder的效率,比string的效率高,stringBuilder不聲明新的區域只是擴大原有區域,而string是要聲明新的區域,消耗內存

6.  Array與ArrayList,Hastable

Array 是數組

ArrayList處理字符串時和Array不一樣,動態添刪該ArrayList.Add();

Hashtable鍵值對

7.  接口與抽象類

接口里都是抽象的方法

接口沒有構造函數,

抽象類可以包括能夠哦實現的方法

抽象類有構造函數

8.  i++和++i

   ++i操作數加一   (i)先加,(i加的結果給變量)再用

   i++操作數不先加一  先用(值先給變量),在加(i再加)。

9.重寫與重載

1.重載函數之間的參數類型或個數是不一樣的.重載方法要求在同一個類中.

2.重寫要求返回類型,函數名,參數都是一樣的,重寫的方法在不同類中.

10.&和&&

&位與

&&邏輯與

11.事件與委托

事件是委托的變量,委托不是事件

12.ref,out

ref開始必須賦值

out開始不須直接賦值 ,out必須要重新賦值。

 

六. 解答題

1.    解釋static,void。

static 是靜態類,寄存性變量,是全局變量

void無返回類型

2.    簡述 private、 protected、 public、 internal 修飾符的訪問權限

Public 是公共類,可以在所有項目中訪問

Protected只能在子類和父類之間訪問

Internal在當前項目訪問

Private在當前類中訪問

3.    什么是裝箱和拆箱

裝箱:值類型轉換為引用類型

拆箱:引用類型轉換為值類型

4.      輸入一個英文小寫字母,將它轉換成大寫字母(快速轉換)。

 string a =Convert.ToString(Console.ReadLine());

 Console.WriteLine(a.ToUpper());

5.       * * * *

      * * *

          * *

             * 打印出來(算法)

 

 

 

6.      1!+3!+5!(使用遞歸實現)(算法)

      方法一:

 

                int  A( int  a)
 
                {
 
                         if (a==0 || a==1)
 
                            return  1;
 
                   else
 
                           returna*A(a-1);
 
                }
 
 
調用:
 
       int  sum=0;
 
       for (inti=1;i<=5;i+=2)
 
      {
 
                  sum+=A(i);
 
      }

 

 

方法二:

 

int  a = Convert.ToInt32(Console.ReadLine());
 
int  m = 1; intsum = 0;
 
  for ( int  i = 1; i <= a; i++)
 
  {
 
      m *= 2 * i - 1;
 
      sum += m;
 
  }
 
  Console.WriteLine(sum);

 

 

7.      輸入5個數,將其重小到大排列輸出。(使用冒泡)(算法)

 

Console.WriteLine( "請按1,2,3,4,5的格式輸入五個數:" );
 
string   str = Convert.ToString(Console.ReadLine());
 
string [] s  = str.Split( ',' );
 
int  length = s.Length-1;
 
int [] num =newint[s.Length];
 
for ( int  i =0 ;i<s.Length; i++)
 
{
 
      num[i] = int .Parse(s[i]);
 
}
 
for ( int  i =0;i<length;i++)
 
{
 
   for ( int  j =0;j<length-i;j++)
 
  {
 
        int  temp;
 
         if ( num[j]>num[j+1])
 
     {
 
          temp =num[j+1];
 
          num[j+1]=num[j];
 
          num[j]=temp;
 
      }
 
  }
 
}
 
foreach ( int  i in  num)
 
{
 
  Console.Write(i+ "\0" );
 
}

 

 

8.      定義一個事件,當我激發它時,返回一個結果給我。(算法)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class  Event1
{
        public  delegatestring Eve();
 
        public  event  Eveeve;
 
        public  stringTrigger()
        {
           if (eve!= null )
                return  eve();
           else
               return  "" ;
       }
      
      public  stringA1()
      {
              return  "A1" ;
      }
      
      public  stringA2()
      {
              return  "A2" ;
      }
}
 
調用:
Event1 event1= new  Event1();
event1.eve+=event1.A1;
MessageBox.Show(event1.Trigger());

 

 

ten

筆試試卷

C#基礎

1.  傳入某個屬性的set方法的隱含參數的名稱是什么?

value

 

2.  如何在C#中實現繼承?

繼承類或接口

 

3.  C#支持多重繼承么?

支持多層繼承,不支持多繼承

 

4.  被protected修飾的屬性/方法在何處可以訪問?

子類或當前類

 

5.  私有成員會被繼承么?

不能

 

6.  請描述一下修飾符protected internal。

在子類和當前程序集可見

 

7.  C#提供一個默認的無參數構造函數,當我實現了另外一個有一個參數的構造函數時候,還想保留這個無參數的構造函數。這樣我應該寫幾個構造函數?

2個

 

8.  C#中所有對象共同的基類是什么?

Object類(超級類)

 

9.  重載和覆寫有什么區別?

1 重載是在當前類進行,而重寫是在子類中;

2 重載的方法返回值可以不同,方法名相同,參數列表不同,而重寫方法名和參數列表及返回值都相同

 

10.   在方法定義中,virtual有什么含意?

定義為虛方法,以便於子類重寫或隱藏

 

11.   能夠將非靜態的方法覆寫成靜態方法么?

(不能)

但是可以寫成靜態方法關鍵字new

 

12.   可以覆寫私有的虛方法么?

不能

 

13.   能夠阻止某一個類被其他類繼承么?

可以,將該類加上sealed申明可實現

 

14.   能夠實現允許某個類被繼承,但不允許其中的某個方法被覆寫么?

可以,將不允許被重寫的方法申明為私有方法或靜態方法

 

15.   什么是抽象類(abstract class)?

是對現實世界中某個類型的一個抽象,例如可將一棵樹定義為一個抽象類,各種樹有不同的特性(高度,樹形等),也有相同的部分(都由樹枝樹干組成等)。不能實例化,只能通過繼承實現。抽象類中即可有抽象方法(以便個性的實現),也有實現方法(共性)。

(若干個類的共同特征。)

 

16.   何時必須聲明一個類為抽象類?

當一個類的不同對象既有相同的共性的地方又有個性的地方時,可將共性的方法設計為實現方法,將其個性的地方設計為抽象方法

若干個類有共同的屬性和方法,事件時候必須聲明一個抽象類)

 

17.   接口(interface)是什么?

(接口是一種服務的描述,然后去實現它。)

定義各種抽象方法的一種機制,不同於類的是,接口可以多繼承,接口不能實例化,通過繼承實現接    口。實現接口必須要實現接口中的所有方法。

 

18.   為什么不能指定接口中方法的修飾符?

接口中的方法均為抽象方法,並且其默認為public,接口中的方法只有一個定義,沒有實現,要使用其方法必須要繼承該接口。而不能實例化接口,以獲得該方法。

 

19.   可以繼承多個接口么?

可以

 

20.   那么如果這些接口中有重復的方法名稱呢?

可以在調用時加上其接口的前綴來唯一標示它

在接口方法名稱前面帶上一個接口名稱)

 

21.   接口和抽象類的區別是什么?

1 接口中的方法都是抽象方法,而抽象類中可以有實現的方法

2 接口中無構造函數,而抽象類有

3 接口方法在子類重寫不需要Override關鍵字修飾

4 繼承接口的類必須實現接口的所有方法,而繼承抽象類的類不需要實現父類的所有抽象方法

5.接口默認的是一個public方法 不能有訪問修飾

接口和抽象類均不能實例化new)

 

22.   如何區別重載方法?

一個方法非重載版本返回值和參數列表可以不同

 

23.   const和readonly有什么區別?

Const為編譯常量,編譯后即不可更改,已經賦值;

Readonly為執行常量,在運行時賦值。

 

24.   System.String 和System.StringBuilder有什么區別?

System.String 的值不可修改,對System.String 的對象賦值是對其引用賦值;

對System.StringBuilder賦值,將直接對該對象的值進行修改,當需要進行大批量的字符串操作時其效率很高。

 

String.string返回新的值

String.StringBuilder不返回原始值

 

25.   如何理解委托?

委托是一種將方法封裝的機制,它可以把方法當做參數進行傳遞和調用。可同時委托多個方法形成委托連,來關聯多個事件或方法。(委托是一用來存儲方法的,可以把方法當做變量傳遞。存儲方法時候會檢查方法的聲明) 



Asp.Net

1.什么是服務器端控件?請舉例。這些控件與一般的Html控件在使用上有何區別?

可以再服務器端運行的控件,比如Button,Text,DropDownList等,一般的Html控件只能激發客戶端事件,而服務端控件激發后會發生回傳,將控件的狀態回傳服務器端並激發相關的服務器控件的事件。

 

服務器控件被標識為ruant:server;而html不會被標識。

 

2.GridView數據源可以有哪些?

1 數據集(包括DateSet,DataTable等)

2 集合(鍵值對,哈希表,字典,隊列等)

3.xml 文件

 

3.什么是用戶控件?

用戶自定義的控件集合,可通過將多個功能關聯的控件組織在一起(如登錄要用到的各種控件),形成用戶控件以提高代碼的重用性

 

(繼承自UserControl)

 

4.什么是code-Behind技術?(后台)

即代碼后置技術,完成前后台代碼的分離,方便維護和代碼管理

aspx,ascx,然后是程序代碼邏輯文件,.cs,.vb,文件、將程序邏輯和頁面分離開)  

 

5.向服務器發送請求有幾種方式?

get和Post表單提交方式

 

6.在頁面中傳遞變量有幾種方式?

1 session(跨頁面)

2 cookie(跨頁面)

3 application (存在一定的風險,登錄所有人使用一個application)

4 viewstate(當前頁面)

 

7.Machine.Config和Web.Config是什么關系?

Machine.Config包括Web.Config,包含和被包含的關系

所頁面繼承Machine.Config,web.config某些項目和網站做些特殊的配置)

 

8.什么是強類型DataSet?如何使用?采用它對分層架構有什么作用?

強類型DataSet定義時即將確定其數據類型,用強類型DataSet定義數據庫的實體,在業務邏輯層即可將它當做類來處理,使用起來就象操作類一樣。它可盡量降低系統架構的耦合。

(DataSet1 dt = new DataSet1();)

強類型DataSet是指需要預先定義對應表的各個字段的屬性和取值方式的數據集,對於所有這些屬性都需要從DataSet,DataTable,DataRow繼承,生成相應的用戶自定義類。強類型的一個重要特征,就是開發者可以直接通過操作強類型數據集對象中的域屬性來實現對關系數據對象的操作,而不是向非強類型數據集那樣,使用結果集進行操作。

 

9.ViewState是什么?

它是保存網頁控件狀態的一個對象,在當前頁中可設置及保存相關的控件的狀態,其采用的是保存在請求體中,在服務器和客戶端往返的機制,即其不需要保存在客戶端或長期保持在服務器端,其采用Base64對控件狀態編碼。

 

ViewState是一個隱藏域,保存這個頁面所有服務器的狀態,在服務器和客戶端進行往返,實現一個有狀態的Http協議。

 

10.如何改變ViewState存儲?

添加票據信息

 

11.如何在頁面回發后清空服務器端控件值?

設置其IsPostBack屬性值為TRUE,在調用控件的Clear()方法

跳轉到當前頁面。

(if(!IsPostBack){Response.Redirect(“this.aspx”);})

(if(!IsPostBack){Server.Transfer(“this.aspx”);})

//重寫page的LoadViewState方法

//不加載viewState中的數據。

if(!IsPostBack){protected override voidLoadViewState(object savestatus)

{//base.LoadViewState(savestatus)}}

 

12.如何判斷頁面是否是回發?

通過IsPostBack屬性

 

13.Global.asax是做什么用的?談談你的使用?

可用於站點全局變量的設置,session開始和結束事件的設置等。

在Global.Asax中設置一個全局變量Application,(讓其自增)可用於統計登錄站點人數的統計。

 

14.Asp.net身份驗證有哪些方式?你是如何做的?

有4種:

1 none 無驗證

2 Form驗證(適合局域網及Internet)

3 Windows身份驗證(使用於局域網)

4 PassPort驗證微軟提供,收費)

對身份驗證的設置是在Web.Config中進行的

 

15.請描述Asp.net數據驗證機制。根據驗證邏輯生成對應腳本,驗證時腳本

Asp.Net提供多種服務器端的驗證控件,如是否為空的驗證控件(驗證是否為空值),比較驗證控件(比較兩個控件輸入的值是否相等),范圍驗證控件(驗證一個值是否在一個范圍內),規則驗證控件(如Email和電話號碼的驗證),綜合驗證控件(記錄其他驗證控件的驗證結果信息,相當於一個驗證匯總)

 

16.什么是Ajax技術?.Net2.0中提供了哪些接口支持?你是如何實現Ajax效果的?

異步Javascript 和 XML的英文簡寫,其包括異步無刷新,JavaScript,Dom操作及XML技術的運用,ASP.Net 2.0中集成了相關的Ajax控件(AjaxExtension),通過該空間中的屬性及觸發器的設置可實現一些Ajax效果。

IcallBackEventHander

 

JavaScript

 

1.獲取以下html片段的第二個<span />元素。

<div id=”d1”><span>1</span><span>2</span></div>

 

2.說出以下表達式值:

       typeof(NaN)

      typeof(Infinity)

       typeof(null)

      typeof(undefined)

      Object.constructor

      Function.constructor

 

 

 

3.說出下面這個函數的作用

 

function(destination, source) {

for (property in source) {

   destination[property] = source[property];

  }

    return destination;

};

 

4.說出下面代碼的運行結果

var a = "123abc";

alert(typeof(a++));

alert(a);

 

數據庫

1.列舉ADO.NET主要對象,並闡述其用途。

DataSet(離線數據集,相當於內存中的一張表),Connection(由於數據庫連接操作),Command(由於數據庫更新操作),DataAdaper(數據庫離線狀態下的查詢及更新操作),DataReader(只讀向前的數據查詢對象)

 

2.DataReader和DataSet有什么區別?

DataReader:連線操作對象,只讀向前 大量數據

DataSet:離線操作對象,相當於內存中的一張表,少量數據

 

3.什么是索引?有什么作用?

索引用於標示數據在內存的地址

索引是一個單獨的、物理的數據庫結構,它是某個表中一列或若干列值的集合和相應的指向表中物理標識這些值的數據頁的邏輯指針清單。

 

4.Sql Server2000和Sql Server2005編寫、調試存儲過程有哪些不同?

 

5.如何設計樹狀數據結構?

通過設置標號和父標號,做成動態鏈表形式。

 

6.有以下3個表:

S (S#,SN,SD,SA) S#,SN,SD,SA 分別代表學號、學員姓名、所屬單位、學員年齡

C (C#,CN )  C#,CN 分別代表課程編號、課程名稱

SC ( S#,C#,G ) S#,C#,G 分別代表學號、所選修的課程編號、學習成績

 

a.使用標准SQL嵌套語句查詢選修課程名稱為’稅收基礎’的學員學號和姓名。

Select S#,SN from s

Where S# in

(select S# from SC where S.S#=SC.S# and C# in

(select C# from C where CN=’稅收基礎’

)

)

 

b.使用標准SQL嵌套語句查詢選修課程編號為’C2’的學員姓名和所屬單位。

Select SN ,SD from s where S# in(select S#from SC where C#=’C2’)

 

 

c.使用標准SQL嵌套語句查詢不選修課程編號為’C5’的學員姓名和所屬單位。

Select SN ,SD from s where S# in(select S#from SC where C# is not C2)

 

d.使用標准SQL嵌套語句查詢選修全部課程的學員姓名和所屬單位。

 

e.查詢選修了課程的學員人數。

Select count(*) from S where S# in (select S#from SC )

 

f.查詢選修課程超過5門的學員學號和所屬單位。

Select SN,SD from S JOIN (SELECT SC.S# FROM SC GROUP BYS# having count(*)>5) C ON S.S# =C.S#

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM