今天編寫(xiě)一個(gè)小程序,在命令行輸入一串整形數(shù)據(jù)到整形數(shù)組里面放椰,輸出里面的最高值如蚜。由于C#里的Console.Read()只能讀取下一個(gè)字符型宙,不能實(shí)現(xiàn)多位數(shù)的一次性讀取,如123妆兑,25。因此考慮到不換行一次性輸入刨晴,我采用了convert.ToInt32()的方法。源碼如下:
string str = Console.ReadLine();//讀取一整行數(shù)據(jù)
string[] str1 = str.Split(' ');//定義一個(gè)字符串?dāng)?shù)組,將str字符串以空格符為界分離并存在str1字符串?dāng)?shù)組中
int high_score = 0;
int[] number = new int[8];
for(int i=0;i<8;i++)
{
number[i] = Convert.ToInt32(str1[i]);將字符串?dāng)?shù)組里面的字符串轉(zhuǎn)換為32位整形數(shù)據(jù)
if(number[i]>high_score)
{
high_score = number[i];
}
}
Console.Write("{0}", high_score);