无法执行命令:被杀 [英] Unable to execute command: Killed

查看:30
本文介绍了无法执行命令:被杀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 IBM 的 在线 Swift 沙箱,我正在尝试运行这段代码,但它给了我一个错误:

I'm using IBM's online Swift sandbox and I'm trying to run this code but it's giving me an error:

代码:

var a: Double = 1.0
var b: Double = 2.0
var c: Double = 3.0
var x: Double = 0.0

func x_func(a_var: Double, b_var: Double, c_var: Double) -> Double {

  x = (-b + (b^2 - 4*a*c)^(1/2))/(2*a)
  return x

}

print(x_func(a_var: a, b_var: b, c_var: c))
print(a)
print(b)
print(c)

错误:

<unknown>:0: error: unable to execute command: Killed
<unknown>:0: error: compile command failed due to signal (use -v to see invocation)

有人可以帮我弄清楚出了什么问题吗?我是 Swift 的新手,所以我在这里没有看到错误.

Could someone help me figure out what's wrong? I'm brand new to Swift, so I don't see an error here.

推荐答案

当您想使用指数时,请尝试使用 pow^ 不适用于 Swift那.使用 Swift ^ 产生按位异或 - 不是指数.

Try using pow when you want to use exponents, the ^ doesn't work with Swift for that. With Swift ^ produces bitwise XOR - not exponent.

import Foundation

var a: Double = 1.0
var b: Double = 2.0
var c: Double = 3.0
var x: Double = 0.0

func x_func(a_var: Double, b_var: Double, c_var: Double) -> Double {

    x = (-b + pow((pow(b, 2) - 4*a*c), 1/2))/2*a
    return x

}

确保在使用 IBM Sandbox 或 HackerRank 之类的东西导入 Foundation 框架时!我知道这很容易忘记,如果你忘记了,会很头疼.

Make sure when using things like IBM Sandbox, or HackerRank or whatever to import Foundation framework! I know it's easy to forget and can cause a lot of headache if you do.

此外,除非您在未显示的 x_func 方法中进行额外计算,否则您要求传入的参数甚至不使用.您可以删除属性,也可以将函数更改为不接受任何变量 - 删除属性并将其更改为如下所示可能更有效:

Also, unless you're doing extra computation in your x_func method that you're not showing, you're asking for parameters to be passed in that you don't even use. You can either get rid of the properties, or change the function to not take in any variables - it would probably be more efficient to get rid of the properties and change it to something like this:

import Foundation

func x_func(a: Double, b: Double, c: Double) -> Double {

    x = (-b + pow((pow(b, 2) - 4*a*c), 1/2))/2*a
    return x

}

这篇关于无法执行命令:被杀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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