從鍵盤輸入字符、數字、統計其中數字的個數,若遇到字符串“bye”時,輸出統計結果,結束程序。
基本思想:
可以使用while循環來處理該程序,使用IndexOf函數來判斷字符串中是否含有“bye”字符串,若有則結束循環,并輸出結果。算法中使用了一個變量s來保存處理的字符串。
其源程序代碼如下:
class Class1
{
static void
{
int count=0; //保存數字的個數
string s=""; //保存輸入的字符串
char ch;
while(true)
{
ch = (char)Console.Read();
if(ch>='0' && ch <='9')
count++;
s+=ch.ToString();
if(s.IndexOf("bye")>=0)
break;
}
Console.WriteLine("數字的個數為:{0}",count);
}
}
運行結果如下:
新聞熱點
疑難解答