需要帮助设置类';s实值等于java中传递给方法的参数
如果有任何人可以提供任何帮助与此代码的任何部分,它将不胜感激 首先,我要定义整个类。所以可能是这样的:需要帮助设置类';s实值等于java中传递给方法的参数,java,getter-setter,Java,Getter Setter,如果有任何人可以提供任何帮助与此代码的任何部分,它将不胜感激 首先,我要定义整个类。所以可能是这样的: public Complex(String cStr){ this(cStr.split("(?=\\+)|(?=\\-)")); // splits cStr at + or - into an // array of strings having two elements. The first element of the // resultant arra
public Complex(String cStr){
this(cStr.split("(?=\\+)|(?=\\-)")); // splits cStr at + or - into an
// array of strings having two elements. The first element of the
// resultant array will be the real portion, while the second is the
// imaginary portion. This array is passed to the next constructor.
How do I determine what the complex number itself is?
// TODO: Write a getter called getComplex() that returns this Complex number itself.
// NOTE: you must return a Complex type, not a String type.
}
在这之后,你可以很容易地做你想做的事情:
class Complex {
float real, img;
public Complex(float real, float img) {
this.real = real;
this.img = img;
}
}
你的问题不是很清楚,但我想这就是你想要的。我建议编写一个名为Complex.parseComplex(stringstr)
的静态方法,因为这是java中从字符串解析数字类型的标准方法。
我希望这有帮助 哎呀,我把标题设置错了,对不起,你能提供一些输入字符串和你期望的输出吗?
public Complex(String real, String img) {
this(Float.parseFloat(real), Float.parseFloat(img));
}
public Complex(String str) {
String[] components = str.split("(?=\\+)|(?=\\-)");
this(components[0], components[1]);
}