找出给定问题中的错误 [英] Find the mistake in the given problem

查看:74
本文介绍了找出给定问题中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我最近接受了采访,他们在下面的代码中提出了一个问题,找出错误。



它如果有人能描述的话会很有帮助...



Hi,
I have recently given an interview, where they asked one question to find the mistake in below code.

It would be helpful if anyone can describe...

class Program
    {
        static void Main(string[] args)
        {
            int i = GetDecimalPlaces(0.001);
            Console.Write(i.ToString());
            Console.ReadLine();
        }

        public static int GetDecimalPlaces(double accuracy)
        {
            return Math.Min(0, -(int)(Math.Floor(Math.Log10(accuracy))));
            
        }
    }





对于0.001,答案应为3。



谢谢。



我的尝试:



我不知道Log10。



我也尝试谷歌但没有结果。



answer should be "3" for "0.001".

Thank You.

What I have tried:

I have no idea about Log10.

Also I tried to google but there is no result.

推荐答案

Logarithm10(x)将返回在这种情况下10的基数必须提高以获得值x,

在这种情况下, x是0.001,所以log10(0.001)返回-3,即10 ^( - 3)= 0.001

无论如何,只需更改

Logarithm10(x) will return the power that a base in this case 10, must raise in order to get the value x,
in this case, the x is 0.001, so log10(0.001) return -3, i.e. 10^(-3) = 0.001
Anyway, just change
Math.Min



to


to

Math.Max


使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么。

没有在调试器中魔术,它没有找到bug,它只是帮助你。
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing.
There is no magic in the debugger, it don't find bugs, it just help you to.


这篇关于找出给定问题中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆