C#簡單程序練習
說明:學習之余溫習幾道經典基礎知識題,將其記錄下來,以供初學者參考。
1,題目:求出0-1000中能被7整除的數,並計算輸出每五個數的和:
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();
運行截圖:
題目2:編寫一個類,其中包含一個排序的方法 Sort(), 當傳入的是一串整數,就按照從小到大的順序輸出,如果傳入的是一個字符串,就將字符串反序輸出。
代碼:
Console.Write("輸入字符串");
string Str = Console.ReadLine();
Paixu Pa = new Paixu();
Pa.Sort(Str);
//此類單獨建在cs文件中
class Paixu
{
//數字的從大到小的排序
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);
}
}
運行截圖:

題目3:
編寫一個矩形類,私有數據成員為舉行的長(len)和寬(wid),無參構造函數將len和wid設置為0,有參構造函數設置和的值,另外,
類還包括矩形的周長、求面積、取舉行的長度、取矩形的長度、取矩形的寬度、修改矩形的長度和寬度為對應的形參值等公用方法。
class Rectangle
{
//無參定義len 和wid為0
double length = 0;
double width = 0;
//有參構造函數
public double Length
{
get { return length; }
set { length = value; }
}
public double Width
{
get { return width; }
set { width = value; }
}
//構造函數周長
public double Perimeter
{
get
{
return ((length + width) * 2);
}
}
//構造函數面積
public double Area
{
get
{
return (length * width);
}
}
//方法,取矩形的長度和寬度
public void LenAndWid(double length, double width)
{
this.length = length;
this.width = width;
}
//修改長度對應的形參值
public void ELength(double Len)
{
this.length = Len;
}
//修改寬度對應的形參值
public void EWidth(double Wid)
{
this.width = Wid;
}
//取矩形的長
public double GetLength()
{
return this.length;
}
//取矩形的寬
public double GetWidth()
{
return this.width;
}
}
截圖:
題目4:
1) 輸出字符串長度。 2) 輸出字符串中第一個出現字母a的位置。 3) 在字符串的第3個字符後面插入子串“hello”,輸出新字符串。
4) 將字符串“hello”替換成“me”,輸出新字符串。 5) 以字符“m”為分隔符,將字符串分離,並輸出分離後的字符 串。代碼:(cs文件)
class Class4
{
/*說明:接受長度大於3的字符串
1:輸出字符串的長度
2:輸出字符串中出現第一個字母a的位置
3:在字符串的第三個字符後加入hello,輸出新的的字符串
4:將字符串hello替換成me,輸出新的字符串
5:以m為分隔符將字符串分離,並輸出分離後的字符串
*/
//構造函數獲取輸入的字符串
string str = string.Empty;
public string Str
{
get { return str; }
set { str = value; }
}
//構造方法輸出字符串的長度
public string Length()
{
char[] ss = this.str.ToCharArray();//將輸入的字符串轉換成數組
int len = ss.Length;
return len.ToString();
}
//輸出字符串中第一個字母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();
}
//在字符串的第三個字母後加入字符串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函數:
Console.Write("輸入字符串");
string Mystr = Console.ReadLine();
Class4 str = new Class4();//實例化處理字符串的類
str.Str = Mystr;
Console.Write("1:字符串的長度為{0}\n2:第一次出現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())));
運行截圖

不足之處還望指點!