foreach —(遍歷數組或循環中的字符,以獲取信息)


namespace ConsoleApplication2
{
class Program
{//letter 字母 gigit 數字 symbol 符號
static void Main(string[] args)
{
Console.WriteLine("請輸入字符");
string s = Console.ReadLine();//輸入的字符串用S來接收
int letter = 0;//最開始字母的個數為0
int gigit = 0; //最開始數字的個數為0
int symbol = 0;//最開始字符的個數為0
foreach (char c in s)
{ //foreach 語句用於循環訪問集合以獲取所需信息,語法 foreach(元素類型 c int 遍歷對象s)
if (char.IsLetter(c)) letter++;//提取字符串中的字母
else if (char.IsDigit(c)) gigit++;//提取字符串中的數字
else symbol++;//提取字符串中的符號
}
Console.WriteLine("字母的個數:" + letter + "\t" + "數字的個數:" + gigit + "\t" + "字符的個數:" + symbol);//輸出
Console.ReadLine();
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM