判斷輸入的字符串是否為數字。
namespace讀入一個整數
{
classPRogram
{
staticvoidMain(string[]args)
{
Console.WriteLine("請輸入你的年齡!");
intage=ReadInt();
Console.WriteLine("你剛剛輸入的年齡為"+age);
Console.WriteLine("請輸入你是哪一年出生的?");
intyear=ReadInt();
Console.WriteLine("噢,你是{0}年出生的呀!",year);
Console.WriteLine("請輸入你們班有多少人?");
intcount=ReadInt();
Console.WriteLine("你們班有{0}人",count);
Console.ReadKey();
}
publicstaticintReadInt()
{
intnumber=0;
do
{
try
{
number=Convert.ToInt32(Console.ReadLine());
returnnumber;
}
catch
{
Console.WriteLine("輸入有誤,請重新輸入!");
}
}while(true);
}
}
}
新聞熱點
疑難解答