【问题标题】:Variable Always Ending up to equal 0 C#变量总是以等于 0 C#
【发布时间】:2015-06-01 15:18:58
【问题描述】:

运行此函数时,在计算出距离公式后,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);



        }

【问题讨论】:

  • 你看过Number6Number8Number1Number3Number2Number4的值是什么吗?
  • 是的,您按照 readlines 中的说明填写它们。
  • 既然你在计算两点之间的距离,你不应该在减去两点时 Math.Abs​​()
  • 1.使用Int32 通常不是最佳实践,而是使用int。 2. 那些变量名太可怕了,它会让将来(甚至现在)调试事情变得非常不愉快。有意义的变量名非常有用。 3. 你在做Number10 = Number6 + Number8,这是错误的。应该是Number10 = Number7 + Number9。 (注意我所说的关于调试的内容。)然后你应该在上面做你的Math.Sqrt
  • 单步执行,检查变量。您几乎肯定会发现问题所在。

标签: c# .net visual-studio


【解决方案1】:

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

        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));
        }

【讨论】:

    【解决方案2】:

    虽然我讨厌回答这样的问题,但我觉得您可以在此处很好地了解和解释一些最佳实践

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

    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();
    

    【讨论】:

      猜你喜欢
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多