IF(int)如何以正确的方式进行IF语句 [英] How do I (int) in the right Way for IF statement

查看:509
本文介绍了IF(int)如何以正确的方式进行IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

听起来您有一些编程概念存在问题,您仍然需要了解;你遇到的特殊问题是打字,这是基础知识之一。是这样,这应该理解你缺少的东西: https://en.wikipedia.org/wiki/Type_system#Strong_and_weak_typing [ ^ ]。



请注意,阅读这样的文章并不能真正教授打字或使用编程语言,它只是定义了你需要注意的知识领域。编程本身需要通过学习一种或多种编程语言和进行软件开发来学习。特别是,C#和.NET输入范例结合了不同的方法,但主要范例是静态安全其他人应该更好地被视为补充: https://en.wikipedia。 org / wiki / C_Sharp_%28programming_language%29 [ ^ ]。



特别是 var 不是该语言的主要表现设备,是不是类型,并不总是适用,并且与类型推断相关

https ://en.wikipedia.org/wiki/Type_inference [ ^ ],

https://msdn.microsoft.com/en -us /溴化锂ary / dd233180.aspx [ ^ ]。



换句话说,使用 var 的代码严格等同于具有显式类型的代码在执行类型推断后你可以替换的声明静态地



你的第一个代码示例毫无意义,因为 testa 未定义。在任何地方, if(true)只是奇怪,因为它相当于没有任何if。在第二个示例中,您做了一件坏事,类型转换,这与变量声明无关。它截断2.5到2.这没有任何意义,因为如果你真的想要2,你应该声明 const int a = 2 。 (不要忘记 const 。)这一切都没有意义。你需要声明你想要的东西并控制你的类型,而不是惊讶于你控制的东西。



-SA
It sounds like you have problem with some programming concepts you still have to understand; and the particular issue you faced with is typing, which is one of the basics. Is so, this should understand what you are missing: https://en.wikipedia.org/wiki/Type_system#Strong_and_weak_typing[^].

Note, that that reading the articles like this one won't really teach typing or using programming language, it just define the field of knowledge you need to be aware of. Programming itself needs to be learned by learning one or more programming languages and doing software development. In particular, C# and .NET typing paradigm combined different approaches, but primary paradigms are static, strong, safe and nominative, others should better be considered as supplementary: https://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29[^].

In particular, var is not a primary expressive device of the language, is not a type, is not always applicable and is related to type inference
https://en.wikipedia.org/wiki/Type_inference[^],
https://msdn.microsoft.com/en-us/library/dd233180.aspx[^].

In other words, the code using var is strictly equivalent to the code with explicit type declaration which you can substitute after type inference is performed statically.

Your first code sample makes no sense because testa is not defined. And everywhere, if (true) is just weird, as it is equivalent to not having any "if" at all. In second sample, you are doing bad thing, typecast, which has nothing to do with variable declaration. It truncates 2.5 to 2. It makes no sense, because if you really wanted 2, you should declare const int a = 2. (Don't forget const.) It all makes no sense. You need to declare what you want and control your type, instead of getting surprised that something goes out of your control.

—SA


我不确定你想通过这个代码实现什么。但错误的原因是你宣布变量

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 * testb



a 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语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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