- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 swift 使用 xcode 编写一个简单的应用程序。我会创建一个计算 BMR 的函数,但这里存在一些问题。我该如何修复它们?这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var Age: UILabel!
@IBOutlet var Height: UILabel!
@IBOutlet var Weight: UILabel!
@IBOutlet var agefield: UITextField!
@IBOutlet var heightfield: UITextField!
@IBOutlet var weightfield: UITextField!
@IBOutlet var BMR: UILabel!
@IBAction func Calculate(sender: UIButton) {
var a = "66,0"
var b = "13,7"
var c = (weightfield.text as NSString).floatValue
var d = "5,0"
var e = (heightfield.text as NSString).floatValue
var f = "6,8"
var g = (agefield.text as NSString).floatValue
var result = a + (b * c) + (d * e) - (f * g)
BMR.text = "\(result)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
c、e 和 g 值取决于文本字段“var result”行有一个错误:“找不到接受所提供参数的‘+’的重载”然后,我创建了一个 Playground ,我用实数更改了 c、e 和 g 变量,它起作用了!
var a = 66.0
var b = 13.7
var c = 72.0
var d = 5.0
var e = 178.0
var f = 6.8
var g = 16.0
var result = a + (b * c) + (d * e) - (f * g)
print(result)
有什么想法吗?提前致谢
--
let a:Float = 66.0
let b:Float = 13.7
var c = (weightfield.text as NSString).floatValue
let d:Float = 5.0
var e = (heightfield.text as NSString).floatValue
let f:Float = 6.8
var g = (agefield.text as NSString).floatValue
var result = a + (b * c) + (d * e) - (f * g)
BMR.text = "\(result)"
Hexagon,感谢您的宝贵帮助。这是更改后的代码。没有问题 :D ...但是有 SIGABRT 信号,所以应用程序无法运行。哪里出错了?
--
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Calculator.ViewController 0x7fcf28467e20> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key BMI.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000109f77c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010bae2bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000109f778a9 -[NSException raise] + 9
3 Foundation 0x000000010a395b53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x0000000109ebfd50 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x000000010aaee52b -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x000000010a946718 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
7 UIKit 0x000000010a946d08 -[UIViewController loadView] + 109
8 UIKit 0x000000010a946f79 -[UIViewController loadViewIfRequired] + 75
9 UIKit 0x000000010a94740e -[UIViewController view] + 27
10 UIKit 0x000000010a8622c9 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x000000010a86268f -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x000000010a86ee21 -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x000000010a812457 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
14 UIKit 0x000000010a8151de -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
15 UIKit 0x000000010a8140d5 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x000000010d5f25e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
17 CoreFoundation 0x0000000109eab41c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x0000000109ea1165 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x0000000109ea0947 __CFRunLoopRun + 887
20 CoreFoundation 0x0000000109ea0366 CFRunLoopRunSpecific + 470
21 UIKit 0x000000010a813b42 -[UIApplication _run] + 413
22 UIKit 0x000000010a816900 UIApplicationMain + 1282
23 Calculator 0x0000000109d6dcd7 main + 135
24 libdyld.dylib 0x000000010c23a145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
最佳答案
您正在尝试将 String
值与 Float
值相乘/相加。此外,如果您不键入文字数字,它们将是 Double
,同样不要在没有转换的情况下与 Float
混合。例如,这是可行的:
@IBAction func Calculate(sender: UIButton) {
var a:Float = 66.0
var b:Float = 13.7
var c = (weightfield.text as NSString).floatValue
var d:Float = 5.0
var e = (heightfield.text as NSString).floatValue
var f:Float = 6.8
var g = (agefield.text as NSString).floatValue
var result = a + (b * c) + (d * e) - (f * g)
BMR.text = "\(result)"
}
关于ios - 将 var 转换为 float 以进行数学运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30575643/
这个问题已经有答案了: Can I bind an array to an IN() condition in a PDO query? (23 个回答) 已关闭 5 年前。 任何人都可以看到我如何在
我阅读了关于此的 bash 手册页,但我不明白其中的区别。我对它们进行了测试,它们似乎产生了完全相同的结果。 如果值不是通过命令行参数设置的,我想设置一个变量的默认值。 #!/bin/bash var
我为我的网站开了一家商店,并让它运行起来,但我意识到它无法正确购买商品。换句话说,您不会走进一家商店,拿起一件商品,购买,再次购买,购买,再次购买,等等,以获得想要的数量。你一次捕获他们。我的网站缺少
基本上,我想知道为什么会这样(将列表的内存地址作为参数传递): void init_lista (elemPtr *list) { *list = NULL; } int main(){
看到这个问题:Is there a (built-in) way in JavaScript to check if a string is a valid number?还有这个:jsperf ,其
我有以下字符串: 我想用正则表达式替换所有后面有 px 的数字,并用 X 乘以它们的值。 (X 是一个变量)。 所以如果X=3,结果就是 请注意 X 必须是我将检索到函数的变量 最佳答案 以下代码
这个问题在这里已经有了答案: 关闭 13 年前。 同时 (var != var) System.out.println("循环.."); 执行它..如何声明..var
我只是好奇。我想知道表达式是否有特定原因 var &= expr 行为方式与不同 var = var && expr. 看起来第一个表达式中的表达式正在执行,而不管 var 上的 false 值。 我
我有这个 Ruby 代码: var1 = 10 puts var1.object_id var1 = var1 + 0 puts var1.object_id var1 = var1 + 1 puts
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: demote boost::function to a plain function pointer 所以我
好吧,堆栈溢出, 我花了几个小时来解决 javascript 中的问题(在 NodeJS 服务器中编写),但我真的不明白。 这就是发生的事情: var data = {x: 50}; var temp
首先,我在这里处理 1 和 0,我很清楚 1 == true 和 0 == false。但是我很好奇为什么这不起作用: $var = 1; echo $var; /* 1 */ $var = $var
标题说的是什么:将变量封装在 {}、"" 或 "{}"中是什么意思?我无法在网上找到关于此的任何解释 - 除了使用不会产生任何结果的符号外,我无法引用它们。 这是一个例子: declare -a gr
我需要将信息发送到我的 Html。例如 $(document).ready(function() { var = "'#"+result.tag+"'" // var = '#tag_dinamy
是否可能,如果可以,如何将以下表达式转换为一行? DEV=$(lsblk -no KNAME,MODEL | grep 'ModelNAME') DEV=${DEV%%'ModelNAME'} 简单的
isset($var) == "Test" 和 isset($var) && $var == 'Test" 有什么区别? 最佳答案 这里是一个简短的例子: $var = "Chuck Test"; v
isset($var) == "Test" 和 isset($var) && $var == 'Test" 有什么区别? 最佳答案 这里是一个简短的例子: $var = "Chuck Test"; v
如果我有一个字符串:[Object a:var b:var c:var]; 如何将 a:、b: 和 c: 与正则表达式匹配? 我试过:\[.+\s+(.+:).+\] 但它不适用于 a、b 和 c,它
这个问题在这里已经有了答案: Why does this if-statement combining assignment and an equality check return true? (
我正在 Powershell 中使用 SCSM,但遇到了 if 语句的问题。 我有一个函数,它根据作为变量传递给函数的条件收集数据。 例子: $JMLs1 = collectTickets -crit
我是一名优秀的程序员,十分优秀!