從鍵盤輸入字符、數(shù)字、統(tǒng)計其中數(shù)字的個數(shù),若遇到字符串“bye”時,輸出統(tǒng)計結(jié)果,結(jié)束程序。
基本思想:
可以使用while循環(huán)來處理該程序,使用IndexOf函數(shù)來判斷字符串中是否含有“bye”字符串,若有則結(jié)束循環(huán),并輸出結(jié)果。算法中使用了一個變量s來保存處理的字符串。
其源程序代碼如下:
class Class1
{
static void
{
int count=0; //保存數(shù)字的個數(shù)
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("數(shù)字的個數(shù)為:{0}",count);
}
}
運行結(jié)果如下:
新聞熱點
疑難解答
圖片精選