基礎練習二,基礎練習
//15. 用方法來實現:有一個字串數組:{ "馬龍", "邁克爾喬丹", "雷吉米勒", "蒂姆鄧肯", "科比布萊恩特" },請輸出最長的字串。
//string[] names = { "馬龍", "邁克爾喬丹", "雷吉米勒", "蒂姆鄧肯", "科比布萊恩特"};
//string max = GetLongest(names);
//Console.WriteLine(max);
//Console.ReadKey();
static string GetLongest(string[] names)
{
// string max=names[0]; 這句是先把names數組的第一個元素的值,賦值max,就是說先把第一個數當最大的數,然後在for迴圈裡面和其他的進行比較,如果存在names[i]的值大於max,那麼就重新賦值最大值。。
string max=names[0];
for (int i = 0; i < names.Length; i++)
{
if(names[i].Length>max.Length)
{
max=names[i];
}
}
return max;
//16. 用方法來實現:請計算出一個整型數組的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求:計算結果如果有小數,則顯示小數點後兩位(四捨五入)。
//int[] nums = { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 };
//double avg = GetAvg(nums);
//avg = Convert.ToDouble(avg.ToString("0.00"));
//Console.WriteLine(avg);
//Console.ReadKey();
//求一個整型數組的平均值
static double GetAvg(int[] nums)
{
double sum = 0;
for (int i = 0; i < nums.Length; i++)
{
sum+=nums[i];
}
return sum / nums.Length;
}
//17. 請通過冒泡排序法對整數數組{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }實現升序排序。
//int[] nums = { 1,3,5,7,90,2,4,6,8,10};
//MaoPao(nums);
//foreach (int item in nums)
//{
// Console.WriteLine(item);
//}
//Console.ReadKey();
//17.冒泡排序
static void MaoPao(int[] nums)
{
for (int i = 0; i < nums.Length-1; i++)
{
for (int j = 0; j <nums.Length-1-j; j++)
{
if(nums[j]>nums[j+1])
{
int temp=nums[j];
nums[j]=nums[j+1];
nums[j + 1] = temp;
}
}
}
}
//18。為教師編寫一個程式,該程式使用一個數組儲存30個學生的考試成績,並給各個數組元素指定一個1-100的隨機值,然後計算平均成績。
//好,我們先來定義一個長度為30的數組
//int[] nums=new int[30];
////通過隨機數給數組賦值,我們應該是給數組中德每一個元素賦值,通過for迴圈來實現
////初始化隨機數對象
//Random r = new Random();
//int sum=0;
//for (int i = 0; i < nums.Length; i++)
//{
// //產生的隨機數
// int number = r.Next(0,100);
// //將產生的隨機數賦值給我們數組中的每一個元素
// nums[i] = number;
// sum+=nums[i];
//}
//Console.WriteLine(sum/nums.Length);
//Console.ReadKey();
//19. 有如下字串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年記?” 患者:“七十五歲。” 大夫:“二十歲咳嗽嗎”患者:“不咳嗽。” 大夫:“四十歲時咳嗽嗎?” 患者:“也不咳嗽。” 大夫:“那現在不咳嗽,還要等到什麼時咳嗽?”"】。需求:請統計出該字元中“咳嗽”二字的出現次數,以及每次“咳嗽”出現的索引位置。
/* string str = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年記?” 患者:“七十五歲。” 大夫:“二十歲咳嗽嗎”患者:“不咳嗽。” 大夫:“四十歲時咳嗽嗎?” 患者:“也不咳嗽。” 大夫:“那現在不咳嗽,還要等到什麼時咳嗽?”";
int index=str.IndexOf("咳嗽");//IndexOf 第一次出現的位置
//我們可以直接找到第一”單詞“出現的位置,但是第二個是在我們第一個的基礎上加1再找的,那麼什麼時候就不找了呢,找不著了就不找了,返回值-1
int count = 1;
while (index!=-1)
{
count++;
index = str.IndexOf("咳嗽",index+1);
//這樣會把-1找出來,下面我們來做一下判斷
if(index==-1)
{
break;
}
Console.WriteLine("第{0}次找到咳嗽的位置是{1}",count,index);
}
Console.ReadKey();
*/
//20. 將字串" hello world,你 好 世界 ! "兩端空格去掉,並且將其中的所有其他空格都替換成一個空格,輸出結果為:"hello world,你 好 世界 !"。
/* string s = " hello world,你 好 世界 ! ";
//去除空格用Trim()
s = s.Trim();
//那我們接下來怎麼做呢?好,我們先將字串中德空格都去掉,然後再用一個空格將他們連起來,返回一個數組
string[] sNew= s.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
//用空格將他們連起來 用join,再重新賦值給字串
s = string.Join(" ",sNew);
//列印輸出
Console.WriteLine(s);
Console.ReadKey();
*/
//21. 製作一個控制台小程式。要求:使用者可以在控制台錄入每個學生的姓名,當使用者輸入quit(不區分大小寫)時,程式停止接受使用者的輸入,並且顯示出使用者輸入的學生的個數,以及每個學生的姓名。效果
//學員姓名的集合
/* List<string> listName = new List<string>();
while (true)
{
Console.WriteLine("請輸入學院姓名");
string name = Console.ReadLine();
//將姓名添加到集合中
if (name.ToLower()!="quit")
{
//如果輸入的不是quit,就將輸入的學員姓名添加到集合中
listName.Add(name);
}
else
{
break;
}
}
Console.WriteLine("您一共輸入了{0}個學員的姓名",listName.Count);
//列印每一個姓名
for (int i = 0; i < listName.Count; i++)
{
Console.WriteLine(listName[i]);
}
Console.ReadKey();
*/
//22.題目內容同上題,再增加一個顯示姓“王”的同學的個數,此處不考慮複姓問題。
/* List<string> listName = new List<string>();
while (true)
{
Console.WriteLine("請輸入學院姓名");
string name = Console.ReadLine();
//將姓名添加到集合中
if (name.ToLower() != "quit")
{
//如果輸入的不是quit,就將輸入的學員姓名添加到集合中
listName.Add(name);
}
else
{
break;
}
}
int count=0;
Console.WriteLine("您一共輸入了{0}個學員的姓名", listName.Count);
//列印每一個姓名
for (int i = 0; i < listName.Count; i++)
{
if(listName[i][0]=='王')
{
count++;
}
Console.WriteLine(listName[i]);
}
Console.WriteLine("姓王的同學有{0}個",count);
Console.ReadKey();
*/
//23. 請將字串數組{ "中國", "美國", "巴西", "澳大利亞", "加拿大" }中的內容反轉。然後輸出反轉後的數組。不能用數組的Reverse()方法。
string[] names={ "中國", "美國", "巴西", "澳大利亞", "加拿大" };
//調用函數,將數組傳進去
Change(names);
foreach (string item in names)
{
Console.WriteLine(item);
}
Console.ReadKey();
//23.反轉
static void Change(string[] names)
{
//要反轉,需要將數組中的所有元素的長度/2,再兩兩的進行比較
for (int i = 0; i < names.Length/2; i++)
{
string temp=names[i];
names[i] = names[names.Length - 1 - i];
names[names.Length - 1 - i]=temp;
}
}
//25.什麼是方法的重載
//方法名相同參數不同(參數不同分為兩種:1.類型一樣,個數不一樣;2.個數一樣,類型不一樣)
}