【C#_变量_格式化输出_If语句_微信猜拳_第一篇】

发布于:2024-05-09 ⋅ 阅读:(26) ⋅ 点赞:(0)

练习来自腾讯课堂免费课程

腾讯课堂上的课程,链接如下:
IT·互联网游戏开发其他玩游戏 学C#(编程入门)


1.1.基本输出语句

基本输出语句——知识点总结

  • Console 控制台
  • WriteLine()
  • 在控制台上输出一行信息:
    • 字符串,用英文双引号做区分;
    • 数字不用引号,数字加减做数学运算,而引号内的数字相加得出的是字符串文字;
    • 语句结束需要用英文的分号结束;
Console.Clear();清屏
	Console.Clear();
字符串和数字区分
	Console.WriteLine("英文引号做字符串区分");
	Console.WriteLine(数字类型不用引号做区分);
Console.ReadLine();
	Console.ReadLine();
  • 等待接收用户输入参数,按回车键保存参数,输出,继续运行下一条语句

1.2.变量的基本使用:

  • 示例代码如下:
using System;

namespace 1.2.变量的基本使用
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1;
            num1 = 10;
            num1 = num1 + 10;
			Console.WriteLine(num1);//那么这里输出的是20
			
            double num2 = 152.251f;//double值小数点后面带"f"
			Console.WriteLine(num2);
			
            String num3 = "字符串";
			Console.Clear();
            Console.WriteLine("num3=" + num1);//这里输出的是 “num3=20”
            Console.ReadLine();
        }
    }
}

变量的基本使用——知识点总结

  • 给变量定义名称
  • 为变量赋值
  • 都是用英文分号 ; 结束
  • 下面的代码计算就可以访问变量
            int num1;
            num1 = 10;
            num1 = num1 + 10;
			Console.WriteLine(num1);//那么这里输出的是20

1.3.格式化输出

  • 练习代码如下:
using System;

namespace 1.3.格式化输出
{
    class Program
    {
        static void Main(string[] args)
        {
            string oldName , newName;
			Console.WriteLine("输入原角色名");
			oldName = Console.ReadLine();
			Console.WriteLine("输入新角色名");
			newName = Console.ReadLine();
			Console.WriteLine("原角色名:{0},改完后的新角色名:{1}",oldName ,newName);

            Console.ReadLine();
        }
    }
}

格式化输出——知识点总结

  • 给变量取一个名称,为变量赋值,那么如何在字符串中加入变量名,然后可以输出呢?

1.4.伤害计算

  • 练习代码如下:
using System;

namespace 1.4.伤害计算
{
    class Program
    {
        static void Main(string[] args)
        {
            string shp;
			Console.WriteLine("设定敌人血值:");
			shp = Console.ReadLine();
			
            string satk;
			Console.WriteLine("设定敌人攻击力值:");
			satk = Console.ReadLine();
			
			int hp;
			hp = int.Parse(shp);

			int atk;
			atk = int.Parse(satk);
			
			Console.WriteLine("按回车键对敌人进行一次攻击");
            Console.ReadLine();
			hp = hp - atk;
			Console.WriteLine("玩家对敌人造成了{0}点伤害,敌人当前血量{1}",atk,hp);

			Console.WriteLine("按回车键对敌人进行一次攻击");
            Console.ReadLine();
			hp = hp - atk;
			Console.WriteLine("玩家对敌人造成了{0}点伤害,敌人当前血量{1}",atk,hp);

			Console.WriteLine("按回车键对敌人进行一次攻击");
            Console.ReadLine();
			hp = hp - atk*2;
			Console.WriteLine("玩家对敌人造成了{0}点伤害,敌人当前血量{1}",atk*2,hp);
			
            Console.ReadLine();
        }
    }
}

伤害计算——知识点总结


1.5.阶段练习:交换两个数

using System;
namespace 1.5.阶段练习:交换两个数
{
    class Program
    {
        static void Main(string[] args)
        {
			int a = 3;
			int b = 5;

			int c;
			c = a;
			a = b;
			b = c;
			Console.WriteLine("a={0},b={1}",a,b);

            Console.ReadLine();
        }
    }
}

练习:

  • 计算并输出长方形的周长和面积。
练习1:比较两个数大小
using System;
namespace 练习1:比较两个数大小
{
    class Program
    {
        static void Main(string[] args)
        {
        	#region 比较两个数大小
			int a = 3;
			int b = 5;

            if (a>b)
            {
				Console.WriteLine(">"); 
			}
			else if (a == b)
            {
                Console.WriteLine("=="); 
            }
			else
            {
                Console.WriteLine("<"); 
            }
			#endregion
        }
    }
}
练习2:±*/
using System;
namespace 练习2:+-*/
{
    class Program
    {
        static void Main(string[] args)
        {
        	#region +-*/
			int a = 3;
			int b = 5;
			int c = 0;
			Console.WriteLine("请输入运算符号:"); 
			string op = Console.ReadLine();
            if (op == "+")
            {
            	c = a + b;
			}
			else if (op == "-")
            {
                c = a - b;
            }
            else if (op == "*")
            {
            	c = a * b;
			}
			else if (op == "/")
            {
                c = a / b;
            }
			else
            {
                Console.WriteLine("input error!");
                Console.ReadLine();
                return;
            }
            Console.WriteLine("{0}{1}{2}={3}",a,op,b,c); 
			#endregion
        }
    }
}

2.1.If语句嵌套

If语句嵌套——知识点总结

  • 用if语句判断条件是否成立
  • 关系运算:>,>=,<,<=,!=,==
  • 逻辑运算:$$,||,!

判断一个数是正数还是负数

  • 如果是正数判断是个位数还是不是个位数
  • 如果是负数判断是个位数还是不是个位数
using System;
namespace 2.1.If语句嵌套
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            if (n>0)
            {
                if (n<10)
                {
                    Console.WriteLine("1~9"); 
                }
                else
                {
                    Console.WriteLine(">+10"); 
                }
            }
            else
            {
                if (n==0)
                {
                    Console.WriteLine("0"); 
                }
                else//n>-10
                {
                    if (n>-10)
                    {
                        Console.WriteLine("-1~-9"); 
                    }
                    else
                    {
                        Console.WriteLine("<-10"); 
                    }
                }
            }

        }
    }
}

2.1.If语句嵌套,并列elseif语句

计算BMI

using System;
namespace 计算BMI
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 计算机MBI
            float height = float.Parse(Console.ReadLine());
            float weight = float.Parse(Console.ReadLine());
            float bmi = weight/(height * height);

            Console.WriteLine("Your BMI is {0}" , bmi);

            if (bmi < 18.5f)
            {
                Console.WriteLine("轻");
            }
            else if (bmi < 24)
            {
                Console.WriteLine("正常");
            }
            else
            {
                Console.WriteLine("异常");

                if (bmi < 27)
                {
                    Console.WriteLine("过重");
                }
                else if (bmi < 30)
                {
                    Console.WriteLine("轻微肥");
                }
                else if (bmi < 35)
                {
                    Console.WriteLine("中等肥");
                }
                else
                {
                    Console.WriteLine("中等肥");
                }
            }
            #endregion
        }

    }
}


练习

NPC对话

using System;
namespace NPC对话
{
    class Program
    {
        static void Main(string[] args)
        {
        	#region NPC对话
			Console.WriteLine("1.猜灯谜");
			Console.WriteLine("2.打开商店"); 
			Console.WriteLine("3.再见"); 
			string s = Console.ReadLine();
            if (s == "1")
            {
            	Console.WriteLine("红公鸡,绿尾巴,身体钻到地底下,又甜又脆营养大。"); 
            	Console.WriteLine("1.红萝卜");
            	Console.WriteLine("2.绿萝卜");
				Console.WriteLine("3.红鲤鱼");
            	Console.WriteLine("4.绿鲤鱼");
            	
            	s = Console.ReadLine();
			}
			if (s == "1")
            {
                Console.WriteLine("你答对了!");
            }
            else if (s == "2"
            		|| s == "3"
            		|| s == "4"
            		)
            {
            	Console.WriteLine("加油!");
			}
			#endregion
        }
    }
}

谜语

using System;
namespace NPC对话
{
    class Program
    {
        static void Main(string[] args)
        {
            #region NPC对话

            MainMenu:

            Console.Clear();
            Console.WriteLine("1.猜灯谜");
            Console.WriteLine("2.打开商店"); 
            Console.WriteLine("3.再见"); 
            string s = Console.ReadLine();
            Console.Clear();
            if (s == "1")
            {
                Console.WriteLine("红公鸡,绿尾巴,身体钻到地底下,又甜又脆营养大。"); 
                Console.WriteLine("1.红萝卜");
                Console.WriteLine("2.绿萝卜");
                Console.WriteLine("3.红鲤鱼");
                Console.WriteLine("4.绿鲤鱼");
                Console.WriteLine("5.我再想想");
                
                s = Console.ReadLine();
                if (s == "1")
                {
                    Console.WriteLine("你答对了!");
                }
                else if (s == "2"
                || s == "3"
                || s == "4"
                )
                {
                    Console.WriteLine("加油!");
                }
            }
            else if (s == "2")
            {
                Console.WriteLine("1.红色跌打水");
                Console.WriteLine("2.金光闪闪的肩甲");
                Console.WriteLine("3.一块萝卜饼");

                s = Console.ReadLine();
                if (s == "1")
                {
                    Console.WriteLine("恢复十点血值");
                }
                else if (s == "2")
                {
                    Console.WriteLine("增加了5点防御力");
                }
                else if (s == "3")
                {
                    Console.WriteLine("增加了5点体力值");
                }
            }
            else if (s == "3")
            {
                goto End;
            }
            Console.WriteLine("返回上一层");
            Console.ReadLine();
            goto MainMenu;
            #endregion
        }

    }
}

2.4.比较数值大小

using System;
namespace 练习1:比较两个数大小
{
    class Program
    {
        static void Main(string[] args)
        {
        	#region 比较两个数大小
			int a = 3;
			int b = 5;

            if (a>b)
            {
				Console.WriteLine(">"); 
			}
			else if (a == b)
            {
                Console.WriteLine("=="); 
            }
			else
            {
                Console.WriteLine("<"); 
            }
			#endregion
        }
    }
}


输入三个数最大值

using System;
namespace 2.1
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 三个数最大者
            int a = 3;
            int b = 5;
            int c = 7;
            int d = 2;

            int max = a;
            if (b > max)
            {
                max = b;
            }
            if (c > max)
            {
                max = c;
            }
            if (d > max)
            {
                max = d;
            }
            Console.WriteLine("max = {0}" , max);
            #endregion
        }

    }
}


2.5.计算成绩等级

输入三个数,找出最大数。输入成绩,计算成绩等级。

using System;
namespace 2.1
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 成绩等级
            int s = 898;
            string g = "";
            if (s > 100)
            {
                g = "";
            }
            else if (s >= 90)
            {
                g = "A";
            }
            else if (s >= 80)
            {
                g = "B";
            }
            else if (s >= 70)
            {
                g = "C";
            }
            else if (s >= 60)
            {
                g = "D";
            }
            else if (s >= 0)
            {
                g = "F";
            }
            else
            {
                g = "";
            }
            Console.WriteLine("Grade is {0}" , g);
            #endregion
        }

    }
}


2.6.微信猜拳

2.6.1. 猜随机数字

生成一个1-100的随机数字,给用户5次机会猜这个数字,
每次猜完后提示用户比用户输入的数大还是小。

using System;
namespace 2.6
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 随机猜数字(1~100);
            Console.WriteLine("请猜数字(1~100) : ");
            
            Random rnd;
            rnd = new Random();
            
            int cpu;
            cpu = rnd.Next(10) + 1;
            
            bool ok;
            ok = false;
            //1
            if (ok == false)
            {
            	Console.WriteLine("请猜数字(1~100) : ");
            	int me = int.Parse(Console.ReadLine());
            	
	            if (me == cpu)
	            {
	                ok = true;
	            }
	            else if (me < cpu)
	            {
	                Console.WriteLine("猜小了");
	            }
	            else
	            {
	                Console.WriteLine("猜大了");
	            }
            }
            //2
            if (ok == false)
            {
            	Console.WriteLine("请猜数字(1~100) : ");
            	int me = int.Parse(Console.ReadLine());
            	
	            if (me == cpu)
	            {
	                ok = true;
	            }
	            else if (me < cpu)
	            {
	                Console.WriteLine("猜小了");
	            }
	            else
	            {
	                Console.WriteLine("猜大了");
	            }
            }
            //3
            if (ok == false)
            {
            	Console.WriteLine("请猜数字(1~100) : ");
            	int me = int.Parse(Console.ReadLine());
            	
	            if (me == cpu)
	            {
	                ok = true;
	            }
	            else if (me < cpu)
	            {
	                Console.WriteLine("猜小了");
	            }
	            else
	            {
	                Console.WriteLine("猜大了");
	            }
            }
            //4
            if (ok == false)
            {
            	Console.WriteLine("请猜数字(1~100) : ");
            	int me = int.Parse(Console.ReadLine());
            	
	            if (me == cpu)
	            {
	                ok = true;
	            }
	            else if (me < cpu)
	            {
	                Console.WriteLine("猜小了");
	            }
	            else
	            {
	                Console.WriteLine("猜大了");
	            }
            }
            //5
            if (ok == false)
            {
            	Console.WriteLine("请猜数字(1~100) : ");
            	int me = int.Parse(Console.ReadLine());
            	
	            if (me == cpu)
	            {
	                ok = true;
	            }
	            else if (me < cpu)
	            {
	                Console.WriteLine("猜小了");
	            }
	            else
	            {
	                Console.WriteLine("猜大了");
	            }
            }
			if (ok == true)
            {
            	Console.WriteLine("恭喜答对了");
            }
            else
            {
            	Console.WriteLine("加油,");
            }
            Console.ReadLine();
            #endregion
        }
    }
}