Hello Guys, i有一个关于(int)变量的问题 (我对C#或任何其他更大的编程语言不熟悉) 究竟是什么(int)...的确如何? i下面有一个代码我尝试乘以3和2.5如果某些事情是真的但是如果我只做这样的smth :
Hello Guys, i have a Question about (int) Variables (im new to C# or any other bigger Programming Languages) what exactly does (int)... does? i have a code below i try to multiply 3 and 2.5 if something is true but if i only make smth like this:
var testa = 2.5; var testb = 3; var test = testa * testb; var test2 = 0; if (true) { test2 = testb * testa; };i然后获得CS0266错误 i Google搜索并发现我应该使用(int)...:
i get then an CS0266 Error i Googled and found that i should use "(int)...":
var testa = 2.5; var testb = 3; var test = testa * testb; var test2 = 0; if (true) { //"true" is just a placeholder test2 = (int)testb * (int)testa; }; Console.WriteLine(test); Console.WriteLine(test2);
//Output Console 7,5 6
使用(int)...dosnt帮助很多,因为结果错了 感谢您的帮助;)
Using "(int)..." dosnt Help much because the Result is Wrong Thanks for any Help ;)
推荐答案听起来您有一些编程概念存在问题,您仍然需要了解;你遇到的特殊问题是打字,这是基础知识之一。是这样,这应该理解你缺少的东西: en.wikipedia/wiki/Type_system#Strong_and_weak_typing [ ^ ]。 请注意,阅读这样的文章并不能真正教授打字或使用编程语言,它只是定义了你需要注意的知识领域。编程本身需要通过学习一种或多种编程语言和进行软件开发来学习。特别是,C#和.NET输入范例结合了不同的方法,但主要范例是静态,强,安全其他人应该更好地被视为补充: https://en.wikipedia。 org / wiki / C_Sharp_%28programming_language%29 [ ^ ]。 特别是 var 不是该语言的主要表现设备,是不是类型,并不总是适用,并且与类型推断相关 https ://en.wikipedia/wiki/Type_inference [ ^ ], msdn.microsoft/en -us /溴化锂ary / dd233180.aspx [ ^ ]。 换句话说,使用 var 的代码严格等同于具有显式类型的代码在执行类型推断后你可以替换的声明静态地。 你的第一个代码示例毫无意义,因为 testa 未定义。在任何地方, if(true)只是奇怪,因为它相当于没有任何if。在第二个示例中,您做了一件坏事,类型转换,这与变量声明无关。它截断2.5到2.这没有任何意义,因为如果你真的想要2,你应该声明 const int a = 2 。 (不要忘记 const 。)这一切都没有意义。你需要声明你想要的东西并控制你的类型,而不是惊讶于你控制的东西。
我不确定你想通过这个代码实现什么。但错误的原因是你宣布变量 I am not sure what are you trying to achieve by this code. But the reason of error was when you declared the variable test2 = 0;
它被声明为 int 。现在当你想要做的时候
it was declared as int. Now when you were trying to do
test2 = testb * testa;你试图分配一个 double 值为 int 类型变量。因为,
you were trying to assign a double value to an int type variable. Because,
var testa = 2.5;make
makes
testa * testba double 。 要获得摆脱它,尝试
a double. To get rid of it, try
var test2 = 0.0;在你的第一个例子中。希望你能得到你想要的东西。 干杯。
in your first example. Hope you will get what you want. Cheers.
你应该使用float而不是int,因为将int 3与浮点数2.5将是7.5,这是一个浮点数。 您正在构建变量testa,它是一个浮点数为int,数字2.5现在变为2. test2 =(int)testb *(int)testa; so test2 = 3 * 2即6。 You should use float instead of int because multiplying an int 3 with a float 2.5 would be 7.5 which is a float. You are casting variable testa which is a float to be an int, the number 2.5 now became 2. test2 = (int)testb * (int)testa; so test2 = 3 * 2 which is 6.
更多推荐
IF(int)如何以正确的方式进行IF语句
发布评论