C#基礎2——②類型轉換(Parse, tryParse),數組求最大值&最小值,數組排序(冒泡排序,正常排序),數組的聯繫及擴展,字符串數組練習,數組的反轉練習

一、類型轉換

string s = Console.ReadLine();
int numbers = Convert.ToInt32("4");

第二行代碼要用int 類型的變量來接收,也可以說,這個方法的返回值是int類型的

若想要把用戶輸入的字符串(string)轉換成double類型的

double douNum = double.Parse("45.5");//返回值是double類型的

把用戶輸入的字符串(string)轉換成int類型的int.tryParse

int intNum =0;
bool result = int.tryParse(Console.ReadLine(),out intNum);

第二行代碼在嘗試將 用戶輸入的字符串 轉換成 int類型 的數據
若result 結果爲 true,則說明轉換成功
若result 結果爲false,則說明轉換失敗
這行代碼不會出現異常

實例——一次語文測試後,老蘇讓班長統計每一個學生的成績並計算全班(全班共5人)的平均成績,然後把所有成績顯示出來

Console.WriteLine("請輸入班級人數");
int number = 0;
int sum =0;
if (int.TryParse(Console.ReadLine(), out number))
{
	int[] scores = new int[number];//聲明一個數組用來存放成績,一個人一個人的輸如

	for(int i=0; i<scores.Length; i++)
	{
		Console.WriteLine("請輸入第{0}個人的成績", i+1);
		scores[i] = int.Parse(Console.ReadLine());
		//把用戶輸入的成績轉換爲int類型並存到數組中

		sum += scores[i];//求和
	}
	Console.WriteLine("總成績爲{0},平均成績爲{1}",sum, sum/scores.Length);
}
else
{
	Console.WriteLine("輸入有誤,");
}
Console.ReadKey();


二、數組相關知識

1、求最大值

一般就最大值用臨時變量,每一個值都和臨時變量作比較
除此以外,當所比較的值中有負數時,temp的初始值不能爲0

int[] numbers = new int[] {-1,-98,-56};
int temp = int.MinValue;//當所比較的值中有負數時,temp的初始值不能爲0

for(int i = 0; i < numbers.Length; i++) //numbers 的  length  指的就是數組的長度
{
	if(numbers[i] > temp)//每一個值都和臨時變量作比較
	{
		temp = numbers[i];//若大於臨時變量,則將該值賦給temp
	}
}
Console.WriteLine("最大值爲{0}", temp);
Console.ReadKey();

2、求最大值和最小值

int[] nums = new int[] {2,43,2,65,-23,76,87,32,-90};

int min = int.MaxValue;//最小值
int max = int.MinValue;//最大值

for (int i = 0; i < nums.Length; i++)
{
	if(nums[i]>max)
	{
		max = nums[i];//求最大值
	}

	if(nums[i]<min)
	{
		min = nums[i];//求最小值
	}
}

Console.WriteLine("最大值爲{0}",max);
Console.WriteLine("最小值爲{0}",min);

Console.ReadKey();

3、數組的排序

①冒泡排序

int[] nums = new int[] {98,54,54,322,43,23};

int temp = 0;

for(int j = 0;j < nums.Length - 1; j++)
{
	for(int i = 0; i < nums.Length - 1 - j; i++)
	{
		if(nums[i]>nums[i + 1])
		{
			temp = nums[i];
			nums[i] = nums[i + 1];
			
			nums[i + 1] = temp;
		}
	}	
}

for(int i = 0; i < nums.Length; i++)
{
	Console.Write(nums[i] + "\t");//將數組再遍歷一遍,把數都顯示出來就可以了
}
Console.ReadKey();

②正常排序

int[] nums = new int[] {2,43,3,1,76};

Array.Sort(nums);//Array 是類,Sort方法,進行排序
// Array.Reverse(nums);//這個方法是  反轉,將前一步的結果進行反轉

 //從小到大排序
for(int i = 0; i < nums.Length - 1; i++)
{
	Console.Write(nums[i] + "\t");
}
Console.ReadKey();

Array.Sort(nums);//Array 是類,Sort方法,將nums數組中的數進行排序

Array.Reverse(nums); 這個方法是 反轉,將前一步的結果進行反轉

③倒着輸出

int[] nums = new int[] {2,43,3,1,76};
//倒着輸出
for(int i = nums.Length - 1; i >= 0; i--)
{
	Console.Write(nums[i] + "\t"); 
}
Console.ReadKey();

4、數組的聯繫

計算一個整數數組的所有元素的和

static void Main(string[] arge)
{
	int[] numbers = new int[] {1,2,3,4};

	Program.SumArray(numbers);
	Console.ReadKey();
}	
public static void SumArray(int[]nums)
{
	int sum = 0;//求和
	for(int i = 0; i<nums.Length; i++)
	{
		sum += nums[i];
	}
	Console.WriteLine("和爲{0}", sum);
}	

5、數組的聯繫擴展

從一個整數數組中取出最大的整數,最小整數,總和,平均值 & 改方法後的代碼

static void Main(string[] args)
{
	//從一個整數數組中取出最大的整數,最小整數,總和,平均值

	//int[] nums = new int[] { -12, 90, -56, 89,0,345 };
    //int max = int.MinValue;//求最大值的變量
    //int min = int.MaxValue;//求最小值的變量

    //int sum = 0;//求和
    //for (int i = 0; i < nums.Length; i++)
    //{
    //    if (nums[i]>max)//求最大值
    //    {
    //        max = nums[i];
    //    }

    //    if (nums[i]<min)//求最小值
    //    {
    //        min = nums[i];
    //    }
    //    sum += nums[i];//求和
    //}

    //Console.WriteLine("和是{0},最大值是{1},最小值是{2},平均值是{3}",sum,max,min,sum/nums.Length);

    //Console.ReadKey();	

	Program.ArraySort();//調用類中的方法
	Console.ReadKey();
}

//改方法
public static void ArraySort()
{
	int[] nums = new int[] {-12,90,-56,89,0,345};
	int max = int.MaxValue;//求最大值的變量
	int min = int.MinValue;//求最小值的變量

	int sum =0//求和
	for(int i = 0;i < nums.Length; i++)
	{
		if(nums[i]> max)//求最大值
		{
			max = nums[i];
		}
	
		if(nums[i]<min)//求最小值
		{
			min = nums[i];
		}
		sum += nums[i]; //求和
	} 
}


6、字符串數組練習

①數組裏面都是人的名字,分割成:例如:老楊|老蘇|老鄒
(老楊,老蘇,老鄒,老虎,老牛,老蔣,老王,老馬)

static void Main(string[] args)
{
	string[] names = new string[] {"老楊","老蘇","老鄒","老虎","老牛","老蔣", "老王","老馬"};
	string str = "";
	//減 1 的原因是 最後得老馬 不需要 |,因此循環的時候,就不循環到 老馬 了
	for(int i = 0; i < names.Length - 1; i++)
	{
		str += names[i] + "|";
	}
	//最後一個 老馬後面不需要|
	Console.WriteLine(str + names[names.Length - 1]);
	Console.ReadKey();
}

改方法

static void Main(string[] args)
{
	string[] names = new string[] {"老楊","老蘇","老鄒","老虎","老牛","老蔣", "老王","老馬"};
	//實現只要傳過來一個數組,都可以進行拼接,則括號內需要寫參數
	Program.AddName(names);
	Console.ReadKey();
}

public static void AddName(string[]names)
{
	string str = "";
	for(int i = 0; i < names.Length - 1; i++)
	{
		str += names[i] + "|";
	}
	Console.WriteLine(str + names[names.Length - 1]);
}

7、數組反轉練習

將一個字符串數組的元素的順序進行反轉

{“我”,“是”,“好人”} {“好人”,“是”,“我”},第1個和第length-i-1個進行交換

①簡單方法

string[] text = {"我", "是", "好人" };
for(int i = text.Length -1, i >= 0, i--)
{
	Console.Write(text[i] + "\t");
}
Console.ReadKey();

②稍微高級

string[] text = {"我", "是", "好人" };
Array.Reverse(text);//對數組進行反轉

for(int i = 0, i < text.Length; i++)
{
	Console.Write(text[i] + "\t");
}
Console.ReadKey();

③更加高級

string[] text = {"我", "是", "好人" };
string temp = "";
for(int i = 0; i < text.Length/2; i++)
{
	temp = text[i];
	text[i] = text[text.Length - i - 1];
	text[text.Length - 1 - i] - temp;//用臨時變量進行交換
}
for(int i = 0; i < text.Length; i++)
{
	Console.Write(text[i] + "\t");
}
Console.ReadKey();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章