为什么数学不正确?例如,如果n = 301 [英] Why do the math incorrectly? For example, if n = 301

查看:67
本文介绍了为什么数学不正确?例如,如果n = 301的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string n = Console.ReadLine();
            string m = "";
            for (int i = n.Length - 1; i >= 0; i--)
            {
                m = m + n[i];
            }
            Console.WriteLine("Added number in reverse order: " + m + "\n");
            Console.WriteLine("The result should have been so if the n = 103(i.e reverse view 301):");
            Console.WriteLine("1 * (8^0) = " + (1 * Math.Pow(8, 0)));
            Console.WriteLine("0 * (8^1) = " + (0 * Math.Pow(8, 1)));
            Console.WriteLine("3 * (8^2) = " + (3 * Math.Pow(8, 2)));
            Console.WriteLine("Summ = 1 + 0 + 192 = 193 - correct result");
            double s = 0, s1 = 0;
            Console.WriteLine("\nStarting cycle:\n");
            for (int i = 0; i < m.Length; i++)
            {
                Console.Write(i + " cycle = ");
                s1 = Convert.ToInt64(m[i]) * Math.Pow(8, i);
                Console.WriteLine(s1);
                s = s + (Convert.ToInt32(m[i]) * Math.Pow(8, i));
            }
            Console.WriteLine("Summ = " + s +" - uncorrect result");
            Console.ReadKey();





我尝试过:



转换变量时可能存在问题?请帮助我!



What I have tried:

Maybe there is a problem when converting variable?. Help me, pleace!

推荐答案

在C / C ++ C#中,转换'3'的技术 3 不使用转换函数,它是使用'3'- '0'



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

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

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



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。
In C/C++C#, the technique to convert '3' to 3 is not to use a convert function, it is to use '3'-'0'.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
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 and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


你正在使用 Convert.ToInt(char)。这将返回字符的值(例如,字符1的值为0x31 / 49)。

请参阅 Convert.ToInt32 Method(Char)(系统) [ ^ ]:

You are using Convert.ToInt(char). This will return the value of the character (e.g. the character '1' has the value 0x31 / 49).
See Convert.ToInt32 Method (Char) (System)[^]:
Quote:

ToInt32(Char)方法返回32位签名整数,表示value参数的UTF-16编码代码单元。如果value不是低代理项或高代理项,则此返回值也表示Unicode代码的值。

The ToInt32(Char) method returns a 32-bit signed integer that represents the UTF-16 encoded code unit of the value argument. If value is not a low surrogate or a high surrogate, this return value also represents the Unicode code point of value.





但你想要这个角色所代表的数字。如果您知道该字符是数字,您可以通过减去'0'得到该数字:



But you want the number represented by the character. If you know that the character is a digit, you can get the number by subtracting '0':

s1 = Convert.ToInt64(m[i] - '0') * Math.Pow(8, i);


使用以下代码。

m [i]返回一个字符。因此,当您将其转换为整数时,它实际上会返回该数字的ASCII值。因此'1'变为49.

只需将其转换为字符串即可正常工作。



Use the following piece of code.
m[i] returns a char. So when you convert it into integer it actually returns the ASCII value of the number. so '1' becomes 49.
Just convert it to string and things should work fine.

for (int i = 0; i < m.Length; i++)
{
Console.Write(i + " cycle = ");
s1 = Convert.ToInt64(m[i].ToString()) * Math.Pow(8, i);
Console.WriteLine(s1);
s = s + (Convert.ToInt32(m[i].ToString()) * Math.Pow(8, i));
}





注意:m是一个字符串,但m [i]是例如:m [0] ='1'是一个字符。



Note: m is a string but m[i] is eg: m[0]= '1' is a char.


这篇关于为什么数学不正确?例如,如果n = 301的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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