快速乘法题 - 可可
我还在学习,只是被困住了。我希望用户输入任何数字,结果我的程序将执行以下方程式:(
x = 5*y
y
是用户添加的数字,x
是结果)
我该怎么做这?我不确定是否应该添加 int
或 NSString
。我应该使用哪个,我应该在头文件中输入任何内容吗?
I'm still learning, and I'm just stuck. I want the user to enter any number and in result, my program will do this equation:
x = 5*y
(y
is the number the user adds, x
is outcome)
How would I do this? I'm not sure if I'm suppose to add in an int
or NSString
. Which should I use, and should I enter anything in the header files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,其中一种是数字类型,另一种是文本类型。如何乘以文本? (除了重复之外。)
您需要一个数字类型。
我会警告
int
,因为它只能保存整数。用户无法输入“0.5”并得到 2.5;当您将“0.5”转换为int
时,小数部分会被去掉,只留下整数部分,即 0。然后您将 5 乘以 0,然后返回结果对于用户来说是 0。使用
double
。这是一个浮点类型;因此,它可以保存小数值。是的,但是您输入的内容取决于您是否想要使用绑定(假设您确实在谈论 Cocoa 而不是 Cocoa Touch)。
如果没有绑定,请声明一个到您要从中检索乘数的文本字段的出口,另一个到您要放入乘积的文本字段。向输入文本字段发送
doubleValue
消息以获取乘数,并向输出文本字段发送setDoubleValue:
消息以及乘积。使用绑定,声明两个保存双精度值的实例变量(同样,一个用于乘数,一个用于乘积)以及公开实例变量的属性,然后综合属性,最后绑定文本字段的
value
绑定到这些属性。Well, one of these is a numeric type and the other is a text type. How do you multiply text? (Aside from repeating it.)
You need a numeric type.
I would caution against
int
, since it can only hold integers. The user wouldn't be able to enter “0.5” and get 2.5; when you converted the “0.5” to anint
, the fractional part would get lopped off, leaving only the integral part, which is 0. Then you'd multiply 5 by 0, and the result you return to the user would be 0.Use
double
. That's a floating-point type; as such, it can hold fractional values.Yes, but what you enter depends on whether you want to use Bindings or not (assuming that you really are talking about Cocoa and not Cocoa Touch).
Without Bindings, declare an outlet to the text field you're going to retrieve the multiplier from, and another to the text field you're going to put the product into. Send the input text field a
doubleValue
message to get the multiplier, and send the output text field asetDoubleValue:
message with the product.With Bindings, declare two instance variables holding
double
values—again, one for the multiplier and one for the product—along with properties exposing the instance variables, then synthesize the properties, and, finally, bind the text fields'value
bindings to those properties.如果您要从 UI 检索 NSString,则非常简单:
If you're retrieving the NSString from a UI, then it's pretty simple to do:
这可以在没有任何目标 C 的情况下完成。也就是说,由于 Objective-C 是 C 的超集,因此可以在纯 C 中解决问题。
在上面的
fscanf
负责转换字符( s) 读取标准输入中的数字并将其存储在i
中。但是,如果
char*
中有来自其他来源的字符,并且需要将它们转换为 int,则可以使用– initWithCString 创建一个
然后使用它的NSString*
:encoding:intValue
方法,但在这个特定问题中根本不需要这样做。This can be done without any objective C. That is, since Objective-C is a superset of C, the problem can be solved in pure C.
In the above the
fscanf
takes care of converting the character(s) read on the standard input to a number and storing it ini
.However, if you had characters from some other source in a
char*
and needed to convert them to an int, you could create anNSString*
with the– initWithCString:encoding:
and then use itsintValue
method, but in this particular problem that simply isn't needed.