• 四个数的运算


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace sisisisiis
    {
        class Program
        {
            private static int sun;
            private static int sum;
            private static int x, y, z,w;
            private static string f;
            private static string h;
            private static string j;
            static void Main(string[] args)
            {
                Console.Write("请输入随机数的范围(9-10):");
                string azzz = Console.ReadLine();
                string regex = @"(d+)D+(d+)";
                Match mstr = Regex.Match(azzz, regex);
                int s1 = int.Parse(mstr.Groups[1].Value);
                int s2 = int.Parse(mstr.Groups[2].Value);
                random(s1, s2);
                Console.Write("请输入答案:");
                int l = int.Parse(Console.ReadLine());
                qlgs(f, x, y);
                hlgs(h, z, w);
                qlgs(j, sun, sum);
                if (l == sun)
                {
                    Console.WriteLine("回答正确!");
    
                }
                else
                {
                    Console.WriteLine("回答错误!");
                }
                Console.Read();
            }
            private static int qlgs(string a1,int a2,int a3)
            {
                switch (a1)
                {
                    case "+":
                        sun = a2 + a3;
                        break;
                    case "-":
                        sun = a2 - a3;
                        break;
                    case "*":
                        sun = a2 * a3;
                        break;
                    case "/":
                        if (a3 == 0)
                        {
    
                        }
                        else
                        {
                            sun = a2 / a3;
                        }
                        break;
                }
                return sun;
     
            }
            private static int hlgs(string b1, int b2, int b3)
            {
                switch(b1)
                {
                    case "+":
                        sum = b2 + b3;
                        break;
                    case "-":
                        sum = b2 - b3;
                        break;
                    case "*":
                        sum = b2 * b3;
                        break;
                    case "/":
                        if (b3 == 0)
                        {
    
                        }
                        else
                        {
                            sum = b2 / b3;
                        }
                        break;
                }
                return sum;
            }
            private static void random(int a,int b)
            {
                Random si = new Random();
                x = si.Next(a, b);
                y = si.Next(a, x);
                z = si.Next(a, y);
                w = si.Next(a, b);
                string[] operators = new string[] { "+", "-", "*", "/" };
                f = operators[new Random().Next(0, 4)];
                string[] opera = new string[] { "+", "-", "*", "/" };
                h = opera[new Random().Next(0, 4)];
                j = operators[new Random().Next(0, 4)];
                string z1 = "=";
                string xfyhz ="("+ x + f + y +")"+ h +"("+z +j+w+")"+ z1;
                Console.WriteLine(xfyhz);
    
            }
        }
    }
    

    总结:
    总的来说这个方法很有局限性,没有把所有的情况都包括全,

      string[] operators = new string[] { "+", "-", "*", "/" };
                f = operators[new Random().Next(0, 4)];
                string[] opera = new string[] { "+", "-", "*", "/" };
                h = opera[new Random().Next(0, 4)];

    为什么每次 f 都等于 h 不能这样用吗?

    想了一下三个运算符,四个数,再加上是否带括号如果把所有的情况都列出来,有点不太可能,代码的重复性会更多。所以就用了这样的偷懒的方式!

  • 相关阅读:
    Android KeyCode列表
    贪吃蛇游戏
    二叉树的深度
    二叉树的层次遍历
    二叉树的后序遍历
    二叉树的中序遍历
    《算法》第四版随笔
    踏上计算机网络学习之路
    二叉树的前序遍历
    登上刷题之路
  • 原文地址:https://www.cnblogs.com/lizanqirxx/p/4969841.html
Copyright © 2020-2023  润新知