语句(if else / Random)

  C#:语句     判断,循环,形成循环的分支和循环
                选择控制:if    else,     switch    case
                选择控制:while,do,for.foreach
                跳转语句:break,continue
                异常处理:try,catch,finally

 回顾  bool  判断是否,输出   true、false

(1)关系运算符(是不是)     ==等于;  != 不等于 ;   <> ; <=小于等于 ; >=大于等于。             

(2) 逻辑运算符    ||     或;     &&    并   ;      !   非

 bool b = !(i > 9);    !要用在()前面。                                      

(3) ?:条件运算符     //()内表示关系运算或逻辑运算,返回bool 值,满足时运算:前面,不满足时运算:后面

    if(表达式){…1…}else{…2…} 满足表达式运算1,否则运算2

练习,判断分数。

 Console.Write("分数");            

int i = int.Parse(Console.ReadLine());            

if (i >= 60&i<=100)            

{ if (i >= 80)   { Console.WriteLine("很好"); }  else { Console.WriteLine("及格");} }            

else            

{if (i<50) { Console.WriteLine("使劲努力!"); } else  { Console.WriteLine("还差一点,加油!"); } }             Console.ReadLine();

   if (表达式) { ……}else if{表达式}{……}else{……}
           练习如下:
            Console.Write("数字:");
           double  a = double.Parse(Console.ReadLine());
            if (a >= 10 & a < 100)
            { Console.WriteLine("这是个两位整数"); }
            else if (a < 0)
            { Console.WriteLine("这是个负数"); }
            else if (a >= 100)
            { Console.WriteLine("这是一个三位数"); }
            else
            { Console.WriteLine("这是个10以内的整数"); }
            Console.ReadLine ();

   /*判断是不是标准体重 
            Console.WriteLine("sex:");
            string  a="男";
            string b = Console.ReadLine();
           Console.WriteLine("high:");
           double c =double .Parse ( Console.ReadLine());
           Console.WriteLine("weight:");
           double d = double.Parse(Console.ReadLine());
           if (b == a)
           {
               double e = c - 100 + 3;                                    //此处要分清e.f大小值然后判断
               double f = c - 100 - 3;
               if (f <= d && d <= e)
               { Console.WriteLine("恭喜您,标准!"); }
               else if (d >= e)                   
               { Console.WriteLine("胖"); }
               else
               { Console.WriteLine("瘦"); } 
           }
           else
           {
               double g = c - 110 + 3;                                  //此处要分清g.h大小值然后判断 
               double h = c - 110 - 3;
               if (h <= d && d <= g)
               { Console.WriteLine("恭喜您,标准!"); }
               else if (d >= g)
               { Console.WriteLine("胖"); }
               else
               { Console.WriteLine("瘦"); }
           }
           Console.ReadLine();      */

  /*求解a*x*x+bx+c=0
            Console.Write("输入a:");
            double a = double.Parse(Console.ReadLine());
            Console.Write("输入b:");
            double b = double.Parse(Console.ReadLine());
            Console.Write("输入c:");
            double c = double.Parse(Console.ReadLine());
            double y; double x;
            if (a == 0)
            { Console.WriteLine ("此时不是一元二次方程");}
            else
            {
                if (b * b < 4 * a * c)
                { Console.Write("无解"); }
                else
                {  if (b * b == 4 * a * c)
                { x = -b / (2 * a); Console.Write("x="+x); }
                else
                { x = (-b + Math.Sqrt(b * b - 4 * a * c))/2*a;
                  y=(-b - Math.Sqrt(b * b - 4 * a * c))/2*a ;
                   
                  // Console.WriteLine("x=" + x); Console.WriteLine("y="+y);

                //此时输出,x=…….换行再显示y=……。注意表达方式。
                Console.Write("x="+x +"y="+y);    //这样是在一行显示!                    注意   + 
                }
                  }
            }
           
            Console.ReadLine();    */

/*四舍五入,输出整数。

  Console.Write("输入一个小数:");
           double  a = double.Parse(Console.ReadLine());
            if (a - (int)a >= 0.5)                                                 //注意运算的表示方式!
            {a = (int)a;
             a = ++a;
             Console.WriteLine(a);}
            else
            {a = (int)a;
            Console.Write(a.ToString());}                //.ToString()      转化为字符串类型的快捷方法!
             Console.ReadLine();       */

 

/*和电脑猜拳*/           把问题的包含情况,都用数值变量表示。这样就把现实问题转换成了数学问题!

Console .WriteLine ("请出拳:");          

string human = Console.ReadLine();         

  Console.Write("人VS电脑:");         

  Random a = new Random();          //定义一个随机数生成器!        

   int c = a.Next(3);                 //随机生成一个3以内的整数         

  string computer;                   //定义一个字符串,为电脑出拳        

   if (c == 0)         

  { computer = "剪刀"; }          

else if (c == 1)          

{ computer = "石头"; }          

else         

  { computer = "布"; }         

  Console.WriteLine(human +"VS"+computer );      

     int b ;                            //定义一个变量,用数字代替出拳           

  if (human !="剪刀"&&human !="石头"&&human !="布")

    {Console.WriteLine ("输入错误");}           

  else if(human =="剪刀")       

      {b=0;}           

  else if (human =="石头")           

  {b=1;}            

else           

  {b =2;}             

  //下面是判断输赢的关系运算                                                      

     if ((b-c==1)||(c-b ==2))            

{Console .WriteLine("你赢了");}          

   else if   ((c-b==1)||(b-c==2))          

{Console .WriteLine ("你输了");}          

   else if (b==c )            

{Console .WriteLine ("平局");}            

   else                

Console .WriteLine ("错误");          

   Console.ReadLine();

 

posted @ 2014-10-13 16:21  安定祥和  阅读(356)  评论(0编辑  收藏  举报