程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> abc-學習C#時遇到一些 代碼上的理解困難

abc-學習C#時遇到一些 代碼上的理解困難

編輯:編程綜合問答
學習C#時遇到一些 代碼上的理解困難

string A = "11,9,23,14,36,48,100,99,123,111";
string B = "23,56,46,123,199,323,187,23";
string C = A + "," + B;
char[] buf = { ',' };
string[] datas = C.Split(buf);
string[] abc = new string[1000];
for (int i = 0; i < abc.Length; i++)
{
abc[i] = "";
}
for (int i = 0; i < datas.Length; i++)
{
string strA = datas[i];
int intA = Convert.ToInt32(datas[i]);
if (abc[intA].Length > 0)
{
abc[intA] = abc[intA] + strA + ",";
}
else
{
abc[intA] = strA + ",";
}
}
for (int i = 0; i < abc.Length; i++)
{
textBox3.Text += abc[i];
}
代碼中的
if (abc[intA].Length > 0)
{
abc[intA] = abc[intA] + strA + ",";
}
else
{
abc[intA] = strA + ",";
}
這段代碼是什麼意思?

最佳回答:


string A = "11,9,23,14,36,48,100,99,123,111";//這裡定義了一個字符串
string B = "23,56,46,123,199,323,187,23";//這裡又定義了一個字符串
string C = A + "," + B;//這裡字符串C的字符內容就是字符串A的加B的,也就是C=“11,9,23,14,36,48,100,99,123,111 , 23,56,46,123,199,323,187,23”;
char[] buf = { ',' };//這裡定義了一個字符
string[] datas = C.Split(buf);//這行代碼的意思是用字符buf將字符串C的內容分開也就得到一個字符串數組了:datas={"11","9","23","14","36","48","100","99","123","111","23","56","46","123","199","323","187","23"}
string[] abc = new string[1000];//這個是創建一個數組長度為1000的字符串數組,也就是是數組裡能存1000個字符串;
for (int i = 0; i < abc.Length; i++)
{
abc[i] = "";//這裡是給abc數組裡每個元素賦值,下標是從零開始到999都賦空字符
}
for (int i = 0; i < datas.Length; i++)
{
string strA = datas[i];//循環將datas數組裡的字符串賦給字符串strA
int intA = Convert.ToInt32(datas[i]);//將字符串數組datas裡的字符串轉換成32位整形數據
if (abc[intA].Length > 0)//如果字符串數組abc在角標為intA的字符串長度大於零
{
abc[intA] = abc[intA] + strA + ",";//字符串數組在intA角標位置的值就是該字符串數組在inA角標的字符串加上字符串strA的字符串值
}
else//如果字符串數組abc在角標為intA的字符串長度小於等於零
{
abc[intA] = strA + ",";//字符串數組abc在角標intA的值就是字符串strA的值加上逗號
}
}
for (int i = 0; i < abc.Length; i++)
{
textBox3.Text += abc[i];
}
代碼中的
if (abc[intA].Length > 0)
{
abc[intA] = abc[intA] + strA + ",";
}
else
{
abc[intA] = strA + ",";
}

如果回答對您有幫助,請采納

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved