我的例子:
object main extends App {
val complexNumber1= new ComplexNum(1,1) //Should Not throw an error
val complexNumber2= new ComplexNum(0,1) //Should Not throw an error
val complexNumber3= new ComplexNum(1,0) //Should Not throw an error
val complexNumber4= new ComplexNum(0,0) //Should throw an error
}
我想要达到的目的如下所述,
x = 10
rm(x) # removed x from the environment
x = 10
x %>% rm() # Doesn't remove the variable x
目前我收到前3个条件的错误,第4个条件没有错误。
有人可以帮我理解上面例子中的require方法和正确的解决方案吗?
答案 0 :(得分:1)
您已将require
方法写为real
且imaginary
变量应始终为0. 因此,如果您传递0以外的任何整数值,则会出现以下错误< / strong>
java.lang.IllegalArgumentException:要求失败:Real或Imaginary,任何一个都应该有值
如果您需要Real or Imaginary, either any one should have value
,则应将require
方法定义为
class ComplexNum(val real:Int,val imaginary:Int) {
require((Option(real).isDefined && Option(imaginary).isDefined),"Real or Imaginary, either any one should have value")
}
答案 1 :(得分:1)
逻辑反演的一个简单例子。
require(!(real==0 && imaginary==0),"Real or Imaginary, either any one should have value")
...也可以表示为......
require(real!=0 || imaginary!=0,"Real or Imaginary, either any one should have value")