zoukankan      html  css  js  c++  java
  • if语句~猜拳游戏

    用计算机来生成随机数:

    Random rand = new Random();

    int n = rand.Next(10); //n代表生成一个0-9之间的随机数

    案列:做猜拳的小游戏

    0---剪刀  1---石头  2---布

    思路:电脑随机产生一个数,人输入一个0--2之间的数

           1、如果人出的拳减去电脑随机数是1或-2,则人赢了

           2、如果人出的拳减去电脑随机数是0,则平局

           3、如果人出的拳减去电脑随机数是-1或2,则电脑赢了

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

                string human = Console.ReadLine();

                Random rand = new Random();      //造一个随机数生成器

                int n = rand.Next(3);        //随机生成一个小于3的整数

                string computer;

                int ren;

                if (n == 0)

                {

                    computer = "剪刀";

                }

                else if (n == 1)

                {

                    computer = "石头";

                }

                else

                {

                    computer = "布";

                }

                

            if (human =="剪刀"||human =="石头"||human =="布")

            {

                if(human =="剪刀")

                {

                    ren = 0;

                }

                else if (human == "石头")

                {

                    ren = 1;

                }

                else

                {

                    ren = 2;

                }

            

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

            

                int jieguo = ren - n;

                if (jieguo==1||jieguo==-2)

                {

                    Console.WriteLine("大姐,你赢了!");

                }

                else if (jieguo == 0)

                {

                    Console.WriteLine("平局!");

                }

                else

                {

                    Console.WriteLine("哈哈哈,你输了!");

                }

            }

            else

            {

                Console.WriteLine("请按套路出拳!");

            }

  • 相关阅读:
    leetcode 18 4Sum
    leetcode 71 Simplify Path
    leetcode 10 Regular Expression Matching
    leetcode 30 Substring with Concatenation of All Words
    leetcode 355 Design Twitte
    leetcode LRU Cache
    leetcode 3Sum
    leetcode Letter Combinations of a Phone Number
    leetcode Remove Nth Node From End of List
    leetcode Valid Parentheses
  • 原文地址:https://www.cnblogs.com/dcdgmjzhal/p/4645040.html
Copyright © 2011-2022 走看看