变量总是等于0 C#

时间:2015-06-01 15:18:58

标签: c# .net visual-studio

运行此操作时,在进行数学运算以找到距离公式后,var数字10将始终等于0。我怎样才能解决这个问题。提前致谢

        Int32 Number1;
        Int32 Number2;
        Int32 Number3;
        Int32 Number4;
        Int32 Number5;

        Console.WriteLine("Type in your 1st X coordinate");
        Number1 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Type in your 1st Y coordinate");
        Number2 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Type in your 2nd X coordinate");
        Number3 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Type in your 2nd Y coordinate");
        Number4 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Do you want to use the distance or midpoint formula.(Type 0 for distance and 1  for midpoint)");
        Number5 = Convert.ToInt32(Console.ReadLine());


        Int32 Number6 = Number1 - Number3;
        Int32 Number7 = Number6 * Number6;
        Int32 Number8 = Number2 - Number4;
        Int32 Number9 = Number8 * Number8;
        Int32 Number10 = Number6 + Number8;

        if (Number5 == 0)
        {


           Console.Out.Write("Square Root of");
           Console.Out.Write(Number10);



        }

2 个答案:

答案 0 :(得分:2)

如果使用更合理的变量名称并在赋值点声明变量,如下所示:

        Console.WriteLine("Type in your 1st X coordinate");
        int x1 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Type in your 1st Y coordinate");
        int y1 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Type in your 2nd X coordinate");
        int x2 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Type in your 2nd Y coordinate");
        int y2 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Do you want to use the distance or midpoint formula.(Type 0 for distance and 1  for midpoint)");
        int forumlaSelection = Convert.ToInt32(Console.ReadLine());

        int dx = x1 - x2;
        int dxSquared = dx * dx;
        int dy = y1 - y2;
        int dySquared = dy * dy;
        int dxPlusDy = dx + dy;

        if (forumlaSelection == 0)
        {
            Console.Out.Write("Square Root of");
            Console.Out.Write(dxPlusDy);
        }

然后您可能更有可能看到错误。

看到dxPlusDy?在我看来它应该是这样的:

int dxSquaredPlusDySquared = dxSquared + dySquared;

你的最终输出应该是平方根:

Console.Out.Write(Math.Sqrt(dxSquaredPlusDySquared));

另外,我建议使用double而不是int

        Console.WriteLine("Type in your 1st X coordinate");
        double x1 = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Type in your 1st Y coordinate");
        double y1 = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Type in your 2nd X coordinate");
        double x2 = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Type in your 2nd Y coordinate");
        double y2 = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Do you want to use the distance or midpodouble formula.(Type 0 for distance and 1  for midpoint)");
        int forumlaSelection = Convert.ToInt32(Console.ReadLine());

        double dx = x1 - x2;
        double dxSquared = dx * dx;
        double dy = y1 - y2;
        double dySquared = dy * dy;
        double dxSquaredPlusDySquared = dxSquared + dySquared;

        if (forumlaSelection == 0)
        {
            Console.Out.Write("Square Root of");
            Console.Out.Write(Math.Sqrt(dxSquaredPlusDySquared));
        }

答案 1 :(得分:0)

虽然我不想回答这样的问题,但我觉得你可以在 最佳实践的理解和解释。

  1. 使用有意义的变量名称。 Number[N] 有意义。进行此更改会导致在逻辑中立即发现多个最高缺陷。
  2. 请勿使用Int32,在这种情况下,使用int通常是最佳做法。 (但是,根据您的情况,double更合适。但这种改变是不必要的。)
  3. 您的公式方式关闭。 (更有意义地重命名变量很容易指出这一点。)
  4. 您无法通过无效输入捕获错误。你应该做一些尝试。五个while循环以非常小的开销处理这个问题。
  5. MSDN Int32.TryParse

    以下修改修复了所有这些问题:

    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;
    int option = 0;
    
    Console.WriteLine("Type in your 1st X coordinate");
    while (!int.TryParse(Console.ReadLine(), out x1))
        Console.WriteLine("That number is invalid, please try again:");
    
    Console.WriteLine("Type in your 1st Y coordinate");
    while (!int.TryParse(Console.ReadLine(), out y1))
        Console.WriteLine("That number is invalid, please try again:");
    
    Console.WriteLine("Type in your 2nd X coordinate");
    while (!int.TryParse(Console.ReadLine(), out x2))
        Console.WriteLine("That number is invalid, please try again:");
    
    Console.WriteLine("Type in your 2nd Y coordinate");
    while (!int.TryParse(Console.ReadLine(), out y2))
        Console.WriteLine("That number is invalid, please try again:");
    
    Console.WriteLine("Do you want to use the distance or midpoint formula.(Type 0 for distance and 1  for midpoint)");
    while (!int.TryParse(Console.ReadLine(), out option))
        Console.WriteLine("That number is invalid, please try again:");
    
    int x2x1 = x2 - x1;
    int x2x1Squared = x2x1 * x2x1;
    int y2y1 = y2 - y1;
    int y2y1Squared = y2y1 * y2y1;
    int addedSquaredValues = x2x1Squared + y2y1Squared;
    
    if (option == 0)
        Console.WriteLine("Distance between ({0},{1}) and ({2},{3}) is {4}", x1, y1, x2, y2, Math.Sqrt(addedSquaredValues));
    
    Console.ReadLine();