C#练习
C#簡單程序練習(xí)
???? 說明:學(xué)習(xí)之余溫習(xí)幾道經(jīng)典基礎(chǔ)知識(shí)題,將其記錄下來,以供初學(xué)者參考。
?
1,題目:求出0-1000中能被7整除的數(shù),并計(jì)算輸出每五個(gè)數(shù)的和:
int Count = 0;int Sum = 0;for (int i = 1; i <= 1000; i++){if (i % 7 == 0){Sum += i;Count++;Console.Write("{0} ", i);}if (Count == 5){Console.Write("和為{0} ", Sum);Sum = 0;Count = 0;Console.WriteLine();}}Console.Read();
運(yùn)行截圖:
?
題目2:編寫一個(gè)類,其中包含一個(gè)排序的方法 Sort(), 當(dāng)傳入的是一串整數(shù),就按照從小到大的順序輸出,如果傳入的是一個(gè)字符串,就將字符串反序輸出。?
代碼:
Console.Write("輸入字符串");string Str = Console.ReadLine();Paixu Pa = new Paixu();Pa.Sort(Str);
//此類單獨(dú)建在cs文件中class Paixu{//數(shù)字的從大到小的排序public void Sort(string str){char[] num = str.ToCharArray();bool t = true;for(int i=0;i<num.Length;i++){if(num[i]<'0'||num[i]>'9'){t = false;break;}}if (t == true){for (int i = 0; i < num.Length; i++){for (int j = 0; j < num.Length - 1 - i; j++){if (num[j] > num[j + 1]){char tem = num[j];num[j] = num[j + 1];num[j + 1] = tem;}}}string Num = new string(num);Console.WriteLine(Num);}else{for(int i = 0; i < num.Length / 2; i++){char tem = num[i];num[i] = num[num.Length - 1 - i];num[num.Length - 1 - i] = tem;}string str2 = new string(num);Console.WriteLine(str2);}}
運(yùn)行截圖:
???
題目3:
編寫一個(gè)矩形類,私有數(shù)據(jù)成員為舉行的長(len)和寬(wid),無參構(gòu)造函數(shù)將len和wid設(shè)置為0,有參構(gòu)造函數(shù)設(shè)置和的值,另外,
類還包括矩形的周長、求面積、取舉行的長度、取矩形的長度、取矩形的寬度、修改矩形的長度和寬度為對(duì)應(yīng)的形參值等公用方法。
class Rectangle{//無參定義len 和wid為0double length = 0;double width = 0;//有參構(gòu)造函數(shù)public double Length{get { return length; }set { length = value; }}public double Width{get { return width; }set { width = value; }}//構(gòu)造函數(shù)周長public double Perimeter{get{return ((length + width) * 2);}}//構(gòu)造函數(shù)面積public double Area{get{return (length * width);}}//方法,取矩形的長度和寬度public void LenAndWid(double length, double width){this.length = length;this.width = width;}//修改長度對(duì)應(yīng)的形參值public void ELength(double Len){this.length = Len;}//修改寬度對(duì)應(yīng)的形參值public void EWidth(double Wid){this.width = Wid;}//取矩形的長public double GetLength(){return this.length;}//取矩形的寬public double GetWidth(){return this.width;}}
截圖:
?題目4:
編寫一個(gè)控制臺(tái)應(yīng)用程序,接收一個(gè)長度大于3的字符串,完成 下列功能:
1) 輸出字符串長度。? 2) 輸出字符串中第一個(gè)出現(xiàn)字母a的位置。? 3) 在字符串的第3個(gè)字符后面插入子串“hello”,輸出新字符串。?
4) 將字符串“hello”替換成“me”,輸出新字符串。? 5) 以字符“m”為分隔符,將字符串分離,并輸出分離后的字符 串。代碼:(cs文件)
class Class4{/*說明:接受長度大于3的字符串1:輸出字符串的長度2:輸出字符串中出現(xiàn)第一個(gè)字母a的位置3:在字符串的第三個(gè)字符后加入hello,輸出新的的字符串4:將字符串hello替換成me,輸出新的字符串5:以m為分隔符將字符串分離,并輸出分離后的字符串*///構(gòu)造函數(shù)獲取輸入的字符串string str = string.Empty;public string Str{get { return str; }set { str = value; }}//構(gòu)造方法輸出字符串的長度public string Length(){char[] ss = this.str.ToCharArray();//將輸入的字符串轉(zhuǎn)換成數(shù)組int len = ss.Length;return len.ToString();}//輸出字符串中第一個(gè)字母a的位置public string aLoction(){char[] ss = this.str.ToCharArray();int loc =1;for (int i =0; i < ss.Length; i++){if (ss[i] == 'a')loc = i+1;// break; }return loc.ToString();}//在字符串的第三個(gè)字母后加入字符串hello,并輸出public string addStr(){string addstr = str.Insert(3, "hello");return addstr;}//將hello替換成me,輸出新的字符串public string ediStr(string newstr){string old = "hello";string ne = "me";string edistr = newstr.Replace(old,ne);return edistr;}//用字母m將字符串分割,輸出分割后的字符串public string splitStr(string ss){string[] newstr = ss.Split('m');string aa = string.Empty;for(int i = 0; i < newstr.Length; i++){// Console.Write(newstr[i]);aa = newstr[i];}return aa;}
main函數(shù):
Console.Write("輸入字符串");string Mystr = Console.ReadLine();Class4 str = new Class4();//實(shí)例化處理字符串的類str.Str = Mystr;Console.Write("1:字符串的長度為{0}\n2:第一次出現(xiàn)A或a 的位置是;{1}\n3:插入hello后的字符串{2}\n4:將hello替換成me后的字符串{3}\n", str.Length(), str.aLoction(), str.addStr(), str.ediStr(str.addStr()));Console.Write("5:用m分割后的字符串{0}",str.splitStr(str.ediStr(str.addStr())));
運(yùn)行截圖
不足之處還望指點(diǎn)!
轉(zhuǎn)載于:https://www.cnblogs.com/dongteng/p/6270351.html
總結(jié)
- 上一篇: 狗狗配种要多少钱一次
- 下一篇: 覆水总难收是什么歌啊