C# 練習題 (4)

C# 練習題 (4)

練習題 (4):Reynolds number is calculated using formula (D*v*rho)/mu Where D = Diameter, V= velocity, rho = density mu = viscosity Write a program that will accept all values in appropriate units (Don't worry about unit conversion) If number is < 2100, display Laminar flow, If it's between 2100 and 4000 display 'Transient flow' and if more than '4000', display 'Turbulent Flow' (If, else, then...)

很單純的題目,練習從鍵盤讀取使用者輸入的數值,並計算結果。然後,判斷計算完的結果,利用 if、else 將不同的結果輸出。

程式碼:

   1:  using System;
   2:   
   3:  namespace ReynoldsNumber
   4:  {
   5:      public class Program
   6:      {
   7:          public static void readValue(string name, out double od)
   8:          {
   9:              string s;
  10:              Console.Write("Please input {0}: ", name);
  11:              s = Console.ReadLine();
  12:   
  13:              while (double.TryParse(s, out od) == false)
  14:              {
  15:                  Console.Write("Please input {0}: ", name);
  16:                  s = Console.ReadLine();
  17:              }
  18:          }
  19:   
  20:          public static void Main(string[] args)
  21:          {
  22:              double d = 0.0, v = 0.0, rho = 0.0, mu = 0.0, reynolds = 0.0;
  23:              readValue("diameter", out d);
  24:              readValue("velocity", out v);
  25:              readValue("density", out rho);
  26:              readValue("viscosity ", out mu);
  27:              reynolds = (d * v * rho) / mu;
  28:   
  29:              if (reynolds < 2100)
  30:              {
  31:                  Console.WriteLine("Laminar flow");
  32:              }
  33:              else if ((reynolds >= 2100) && (reynolds < 4000))
  34:              {
  35:                  Console.WriteLine("Transient flow");
  36:              }
  37:              else
  38:              {
  39:                  Console.WriteLine("Turbulent Flow");
  40:              }
  41:          }
  42:      }
  43:  }