課程
/后端開發
/C#
/C#開發輕松入門
一次考試,各位同學的姓名和分數如下:
請編寫程序,輸出分數最高的同學的姓名和分數。運行效果如下:
2018-06-12
源自:C#開發輕松入門 6-1
正在回答
記錄下,最上面的二維數組的厲害了,
if(int.Parse(info[i,1])>int.Parse(info[temp,1]))//用parse轉換成整形,進行循環比較 temp初始為0,條件為真則temp值加1,第一次循環[0,1],和[0,1] 比較條件為false,i+1,而temp不變,之后 [(I)1,1]和[(temp)0,1]比較為真,temp變成1,之后i繼續循環,temp變成2,循環終止后,temp還是2.
temp+1
打印的是temp值,
string[,] info = new string[8, 2] { { "吳松", "89" }, { "錢東宇", "90" }, { "伏晨", "98" }, { "陳陸", "56" }, { "周蕊", "60" }, { "林日鵬", "9" }, { "何昆", "93" }, { "關欣", "85" } };
? ? ? ? ? ? int temp=0;
? ? ? ? ? ? for(int i=0;i<info.GetLongLength(0);i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(int.Parse(info[i,1])>int.Parse(info[temp,1]))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? temp=i;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.Write("分數最高的是" + info[temp, 0] + ",分數是" + info[temp, 1]);
string[,] a = new string[2, 8] { { "吳松", "錢東宇", "伏晨", "陳陸", "周蕊", "林日鵬", "何昆", "關欣" }, { "89", "90", "98", "56", "60", "91", "93", "85" } };
? ? ? ? ? ? int record=0;
? ? ? ? ? ? int max=0;
? ? ? ? ? ? //獲取二維數組里面第二個數組的長度GetLength(1)
? ? ? ? ? ? for (int i = 0; i < a.GetLength(1); i++)
????????????//int.Parse強制轉換成int類型
? ? ? ? ? ? ? ? if (int.Parse(a[1, i]) > max)
? ? ? ? ? ? ? ? ? ? max = int.Parse(a[1, i]);
? ? ? ? ? ? ? ? ? ? record = i;
? ? ? ? ? ? Console.Write("分數最高的是" + a[0, record] + ",分數是" + a[1, record]);
你說的是用二維數組做出來嗎?像這樣?
string[] name = { "吳松", "錢東宇", "伏晨", "陳陸", "周蕊", "林日鵬", "何昆", "關欣" };
? ? ? ? ? ? int[]score = { 89,90,98,56,60,91,93,85};
? ? ? ? ? ? int max = score[0];
? ? ? ? ? ? int record = 0;
? ? ? ? ? ? for (int i = 1; i < score.Length; i++) {
? ? ? ? ? ? ? ? if (score[i]>max) {
? ? ? ? ? ? ? ? ? ? max = score[i];
? ? ? ? ? ? ? ??
? ? ? ? ? ? Console.Write("分數最高的是{0},分數是{1}" + name[record], max);
那個 親,首先謝謝您啊~~然后我再問下那個,我沒理解錯的話這個應該是聲明的兩個數組的方式·~
親,你會那種聲明 string[,] a = new string[,]{~~~~~};
這種二維的嗎??
?string[] x = { "吳松", "錢東宇", "伏晨", "陳陸", "周陸", "周蕊", "林日鵬", "何昆", "關欣" };
? ? ? ? ? ? int[] y = { 89, 90, 98, 56, 60, 91, 93, 85 };
? ? ? ? ? ? int max;
? ? ? ? ? ? max = 0;
? ? ? ? ? ? string name;
? ? ? ? ? ? name = null;
? ? ? ? ? ? for (int i = 0; i < y.Length; i++)
? ? ? ? ? ? ? ? if (y[i] > max || max == 0)
? ? ? ? ? ? ? ? ? ? max = y[i];
? ? ? ? ? ? ? ? ? ? name = x[i];
? ? ? ? ? ? Console.WriteLine("分數最高的是{0},分數是{1}", name, max);
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
?????????? string[] a = new string[] { "吳松", "錢東宇", "伏晨", "陳陸", "周蕊", "林日鵬", "何昆", "關欣" };??????????? int[] b = new int[] { 89, 90, 98, 56, 60, 91, 93, 85 };??????????? int max = 0;??????????? string name = null;??????????? for (int i = 0; i < b.Length; i++)??????????? {??????????????? if (b[i] > max)??????????????? {??????????????????? max = b[i];??????????????????? name = a[i];??????????????? }??????????? }??????????? Console.Write("分數最高的是{0},分數是{1}", name, max);
定義一個string數組存放姓名,一個int數組存放成績,然后定義一個最大值max,用for循環嵌套if比較大小把最大值賦給max,并把最大值的索引給姓名數組。
舉報
本門課程是C#語言的入門教程,將帶你輕松入門.NET開發
3 回答這道題如果用二維數組的話該怎么作?
1 回答我用二維數組做的(菜鳥,勿噴)
1 回答二維數組
2 回答關于二維數組GetLength的用法
1 回答老師在嗎?用二維數組還是一維數組效率比較高呢?我現在用的是二維數組,代碼如下
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-07-09
記錄下,最上面的二維數組的厲害了,
if(int.Parse(info[i,1])>int.Parse(info[temp,1]))//用parse轉換成整形,進行循環比較 temp初始為0,條件為真則temp值加1,第一次循環[0,1],和[0,1] 比較條件為false,i+1,而temp不變,之后 [(I)1,1]和[(temp)0,1]比較為真,temp變成1,之后i繼續循環,temp變成2,循環終止后,temp還是2.
temp+1
打印的是temp值,
2018-07-02
string[,] info = new string[8, 2] { { "吳松", "89" }, { "錢東宇", "90" }, { "伏晨", "98" }, { "陳陸", "56" }, { "周蕊", "60" }, { "林日鵬", "9" }, { "何昆", "93" }, { "關欣", "85" } };
? ? ? ? ? ? int temp=0;
? ? ? ? ? ? for(int i=0;i<info.GetLongLength(0);i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(int.Parse(info[i,1])>int.Parse(info[temp,1]))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? temp=i;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.Write("分數最高的是" + info[temp, 0] + ",分數是" + info[temp, 1]);
2018-06-27
string[,] a = new string[2, 8] { { "吳松", "錢東宇", "伏晨", "陳陸", "周蕊", "林日鵬", "何昆", "關欣" }, { "89", "90", "98", "56", "60", "91", "93", "85" } };
? ? ? ? ? ? int record=0;
? ? ? ? ? ? int max=0;
? ? ? ? ? ? //獲取二維數組里面第二個數組的長度GetLength(1)
? ? ? ? ? ? for (int i = 0; i < a.GetLength(1); i++)
? ? ? ? ? ? {
????????????//int.Parse強制轉換成int類型
? ? ? ? ? ? ? ? if (int.Parse(a[1, i]) > max)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? max = int.Parse(a[1, i]);
? ? ? ? ? ? ? ? ? ? record = i;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.Write("分數最高的是" + a[0, record] + ",分數是" + a[1, record]);
你說的是用二維數組做出來嗎?像這樣?
2018-06-20
string[] name = { "吳松", "錢東宇", "伏晨", "陳陸", "周蕊", "林日鵬", "何昆", "關欣" };
? ? ? ? ? ? int[]score = { 89,90,98,56,60,91,93,85};
? ? ? ? ? ? int max = score[0];
? ? ? ? ? ? int record = 0;
? ? ? ? ? ? for (int i = 1; i < score.Length; i++) {
? ? ? ? ? ? ? ? if (score[i]>max) {
? ? ? ? ? ? ? ? ? ? max = score[i];
? ? ? ? ? ? ? ? ? ? record = i;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? Console.Write("分數最高的是{0},分數是{1}" + name[record], max);
2018-06-13
那個 親,首先謝謝您啊~~然后我再問下那個,我沒理解錯的話這個應該是聲明的兩個數組的方式·~
親,你會那種聲明 string[,] a = new string[,]{~~~~~};
這種二維的嗎??
2018-06-13
?string[] x = { "吳松", "錢東宇", "伏晨", "陳陸", "周陸", "周蕊", "林日鵬", "何昆", "關欣" };
? ? ? ? ? ? int[] y = { 89, 90, 98, 56, 60, 91, 93, 85 };
? ? ? ? ? ? int max;
? ? ? ? ? ? max = 0;
? ? ? ? ? ? string name;
? ? ? ? ? ? name = null;
? ? ? ? ? ? for (int i = 0; i < y.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (y[i] > max || max == 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? max = y[i];
? ? ? ? ? ? ? ? ? ? name = x[i];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine("分數最高的是{0},分數是{1}", name, max);
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
2018-06-13
那個 親,首先謝謝您啊~~然后我再問下那個,我沒理解錯的話這個應該是聲明的兩個數組的方式·~
親,你會那種聲明 string[,] a = new string[,]{~~~~~};
這種二維的嗎??
2018-06-13
?????????? string[] a = new string[] { "吳松", "錢東宇", "伏晨", "陳陸", "周蕊", "林日鵬", "何昆", "關欣" };
??????????? int[] b = new int[] { 89, 90, 98, 56, 60, 91, 93, 85 };
??????????? int max = 0;
??????????? string name = null;
??????????? for (int i = 0; i < b.Length; i++)
??????????? {
??????????????? if (b[i] > max)
??????????????? {
??????????????????? max = b[i];
??????????????????? name = a[i];
??????????????? }
??????????? }
??????????? Console.Write("分數最高的是{0},分數是{1}", name, max);
2018-06-13
定義一個string數組存放姓名,一個int數組存放成績,然后定義一個最大值max,用for循環嵌套if比較大小把最大值賦給max,并把最大值的索引給姓名數組。