条件语句
猜拳
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 猜拳 { class Program { static void Main(string[] args) { while (true) { //用户输入石头剪子布,计算器也随机出一个石头剪子布, //用户与计算机进行对比,得出胜负; //1、用户输入选择的手势 Console.Write("请输入您的手势(石头:0,剪子:1,布:2):"); int yh = Convert.ToInt32(Console.ReadLine()); //2、计算机随机选出计算机的手势 Random r = new Random(); int dn = r.Next(0, 3); //3、对比,输出结果 //石头0,剪子1,布2 //0 1 = -1 //1 2 = -1 //2 0 = 2 //0 2 = -2 //1 0 = 1 //2 1 = 1 int jg = yh - dn; if (jg == -1 || jg == 2) { Console.WriteLine("用户胜利!"); } else if (jg == -2 || jg == 1) { Console.WriteLine("用户失败!"); } else { Console.WriteLine("平局!"); } } Console.ReadLine(); } } }
随机数
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 随机数 { class Program { static void Main(string[] args) { Random r = new Random(); Console.WriteLine(r.Next(0, 1)); Console.WriteLine(r.Next(0, 1)); Console.WriteLine(r.Next(0, 1)); Console.WriteLine(r.Next(0, 1)); Console.WriteLine(r.Next(0, 1)); Console.WriteLine(r.Next(0, 1)); Console.WriteLine(r.Next(0, 1)); Console.WriteLine(r.Next(0, 1)); Console.WriteLine(r.Next(0, 1)); Console.WriteLine(r.Next(0, 1)); Console.ReadLine(); } } }
小练习:身高体重
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 条件语句练习 { class Program { static void Main(string[] args) { //男士体重=身高-100+-3 //女士体重=身高-110+-3 //KG CM //90 180 //180-100 = 80 //男士体重 - 男士身高 = -100+-3; //男士体重 - 男士身高 +100 =+-3; //内容输入 Console.Write("请输入您的性别:"); string x = Console.ReadLine(); Console.Write("请输入您的身高(cm):"); int g = Convert.ToInt32(Console.ReadLine()); Console.Write("请输入您的体重(kg):"); int z = Convert.ToInt32(Console.ReadLine()); //数据运算+数据输出 if (x == "男") { //男士体重=身高-100+-3 int bz = g - 100; if (z > bz + 3) { Console.WriteLine("你偏胖了!"); Console.WriteLine("您超出了标准体重" + (z - (bz + 3)) + "公斤!"); } else if (z < bz - 3) { Console.WriteLine("你偏瘦了!"); Console.WriteLine("您距离标准体重还差" + (bz - 3 - z) + "公斤!"); } else if (z <= bz + 3 && z >= bz - 3) { Console.WriteLine("你身材很标准!继续保持!"); } } else if (x == "女") { //女士体重=身高-110+-3 int bz = g - 110; if (z <= bz + 3 && z >= bz - 3) { Console.WriteLine("您身材很标准!继续保持!"); } else if (z > bz + 3) { Console.WriteLine("您偏胖了!"); } else { Console.WriteLine("您偏瘦了!"); } } else { Console.WriteLine("性别输入有误!"); } Console.ReadLine(); } } }