方法/函數(shù)3(對用戶輸入的數(shù)據(jù)進(jìn)行)
public static string Isyesorno(string input)//這是個簡單的程序,只能輸入yes 或者no
{
? ? ? ? ?while (true)
? ? ? ? ?{
? ? ? ? ? ? ? ???if (input == "yes" || input == "no")
? ? ? ? ? ? ? ? ?{ return input;}
? ? ? ? ? ? ? ? else?
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ?Console.WriteLine("只能輸入yes或者no,請重新輸入");
? ? ? ? ? ? ? ? ? ? ? ?? input = Console.ReadLine();
? ? ? ? ? ? ? ? }
? ? ? ? }
}//自定義函數(shù)結(jié)束
static void Main(string[] args)//進(jìn)入主函數(shù)
{
? ? ? ? ?Console.WriteLine("請輸入yes或者no");
? ? ? ? ?string a = Console.ReadLine();
? ? ? ? ?string result=Isyesorno(a);//引用了自定義函數(shù)
? ? ? ? ?Console.WriteLine(result);//輸出結(jié)果。
?}
第二個示范案例:
?public static double Luoqiheight(double a)//計(jì)算落地彈起的高度,每次彈起為落地的一半
{
? ? ? ? ?int i = 1;
? ? ? ? ?while (i < 11)
? ? ? ? ?{? a /= 2.0 ;? i++ ; }
? ? ? ? ? return a;
}
static void Main(string[] args)
{
? ? ? ? Console.WriteLine("請輸入落地高度:");
? ? ? ? double n = Convert.ToInt32(Console.ReadLine());
? ? ? ? double n1 = Luoqiheight(n);
? ? ? ? Console.WriteLine("落起高度是:"+n1);
}
總結(jié):程序的輸入和輸出語句,寫在主函數(shù),方法、執(zhí)行語句寫在自定義函數(shù)。