zoukankan      html  css  js  c++  java
  • 函数——返回值不止一个的情况(解一元二次方程)

    namespace 一元二次方程函数解决
    {
        class Program
        {
            static void Main(string[] args)
            {
                while (true)
                {
                   
                Console.WriteLine("请为一元二次方程ax²+bx+c设置相关参数:");
                Console.WriteLine("请输入a:");
                int a = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入b:");
                int b = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入c:");
                int c = Convert.ToInt32(Console.ReadLine());
                double x1=0;  //定义两个变量用来接收out的x1与x2
                double x2=0;
                string s=  new Program().solve(a,b,c,out x1,out x2);    //调用函数solve 输入参数a,b,c用来进行函数运算及 除返回值外的参数 out x1 与out x2
                Console.WriteLine(s);
                Console.WriteLine("第一个根:"+x1);
                Console.WriteLine("第二个根:"+x2);
                Console.ReadLine();
                }

            }
            /// <summary>
            /// 解方程
            /// </summary>
            /// <param name="a"></param>
            /// <param name="b"></param>
            /// <param name="c"></param>
            /// <returns></returns>
            public string solve(int a, int b, int c,out double x1,out double x2)  //定义一个函数 用来解方程
            {
                string s;
                int x=b*b-4*a*c;
                if (a > 0 || a < 0)
                {  
                    if(x>0)
                    {  
                        s = "该方程有两个不等的根"; 
                    }
                    else if(x==0)
                    {
                        s = "该方程有两个相等的根";  
                    }
                    else
                    {
                        s = "该方程无根";
                    }
                }
                else
                {
                    s = "该方程无根";
                }
                x1 = (-b + Math.Sqrt(x)) / (2 * a);  //Math.Sqrt(); 求平方根
                x2 = (-b - Math.Sqrt(x)) / (2 * a);
                return s;  //返回一个string类型的s
            }
        }
    }

  • 相关阅读:
    macbook466加了两条1333金士顿正常
    spring 使用 groovy 的 utf8 问题
    jQuery Pagination Plugin Demo
    ssh 二级跳 转
    实战 Groovy: 用 curry 过的闭包进行函数式编程
    无刷新分页 jquery.pagination.js 张龙豪 博客园
    用fgets()函数从屏幕上输入一字符串_BenRuanChinaUnix博客
    What Is My IP Shows Your IP Address
    Chapter 24. Dynamic language support
    什么是SQA?
  • 原文地址:https://www.cnblogs.com/lk-kk/p/4423722.html
Copyright © 2011-2022 走看看