本文介绍了C#二次方程求解器有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚编写了我的第一个 C# 程序.

I just wrote my first C# program.

这是一段求解二次方程的简单代码.

It's a simple piece of code which solves quadratic equations.

它与某些函数(例如 -6x2-6x+12)完美配合,而对于其他函数(4x2-20x+25),它表现出我怀疑是舍入误差.

It works with some functions (such as -6x2-6x+12) perfectly, while with others, (4x2-20x+25) it exhibits what I suspect are rounding errors.

我对 C# 完全陌生,我看不到任何问题;有人能帮我调试这段代码吗?

I'm completely new to C#, and I can't see an problems; would someone be able to help me debug this code?

namespace ConsoleApplication {
    class Program {
        static int ObtainInput(string prompt, bool canBeZero) {
            double a = ObtainInput("A? ", false);
            double b = ObtainInput("B? ", true);
            double c = ObtainInput("C? ", true);
            double d, x1, x2;

            while (true) {
                Console.Write(prompt);
                string input = Console.ReadLine();
                int result;
                bool success = int.TryParse(input, out result);
                if (success && (canBeZero || result != 0))
                    return result;
                Console.WriteLine("Invalid input!");
            }

            // Calculating a discriminant
            d = b * b - 4 * a * c;

            if (d == 0) {
                x1 = -b / (2 * a);
                Console.WriteLine("The only solution is x={0}.", x1);
                Console.ReadLine();
            }

            // If d < 0, no real solutions exist
            else if (d < 0) {
                Console.WriteLine("There are no real solutions");
                Console.ReadLine();
            }

            // If d > 0, there are two real solutions
            else {
                x1 = (-b - Math.Sqrt(d)) / (2 * a);
                x2 = (-b + Math.Sqrt(d)) / (2 * a);
                Console.WriteLine("x1={0} and x2={1}.", x1, x2);
                Console.ReadLine();
            }
        }
    }
}

推荐答案

棒极了.现在是不养成坏习惯的好时机:

Awesome. Now would be a great time to not get into bad habits:

entA: Console.Write("a?");
try { a = Convert.ToInt32(Console.ReadLine()); }
catch
{ /*If a=0, the equation isn't quadratic*/
  Console.WriteLine("Invalid input");
  goto entA;
}

问题比比皆是.首先,使用 int.TryParse,而不是使用 try-catch 来解决可能失败的问题.

Problems abound. First off, use int.TryParse, rather than putting a try-catch around something that can fail.

其次,注释与代码的动作不匹配.代码确定结果是否为整数;评论说它检查零.

Second, the comment does not match the action of the code. The code determines if the result is an integer; the comment says that it checks for zero.

第三,当您试图表示的是循环时不要使用 goto.

Third, do not use a goto when what you are attempting to represent is a loop.

第四,看看所有重复的代码!您将相同的代码重复了 3 次,但略有不同.

Fourth, look at all that duplicated code! You have the same code repeated three times with minor variations.

让自己成为一个辅助方法:

Make yourself a helper method:

 static int ObtainInput(string prompt, bool canBeZero)
 {
     while(true) // loop forever!
     {
         Console.Write(prompt);
         string input = Console.ReadLine();
         int result;
         bool success = int.TryParse(input, out result);
         if (success && (canBeZero || result != 0))
             return result;
         Console.WriteLine("Invalid input!");
     }
 }

现在你的主线是:

int a = ObtainInput("A? ", false);
int b = ObtainInput("B? ", true);
int c = ObtainInput("C? ", true);

你的错误在这里:

x1 = x2 = -b / (2 * a);

您以整数进行算术运算,然后然后转换为双精度数.也就是说,您进行除法,四舍五入到最接近的整数,然后转换为双精度.从一开始就用双打(或者不太可能是小数).应该是:

You do the arithmetic in integers, and then convert to doubles. That is, you do the division, round to the nearest integer, and then convert to double. Do it in doubles (or, less likely, in decimals) from the start. It should be:

double a = ObtainInput("A? ", false);
double b = ObtainInput("B? ", true);
double c = ObtainInput("C? ", true);

也就是说,a、b 和 c 不应该是整数.

That is, a, b, and c should not ever be integers.

这篇关于C#二次方程求解器有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 17:24