现在,我正在开发一个GUI,它将根据用户在文本字段中输入的内容来计算运动学的值。我创建了一个带有Double (不是double)类型的值的私有内部类,然后创建了一个方法来根据给定的值获取值。例如,这将返回初始速度:
public Double getInitialVelocity(Double vf, Double a, Double ti, Double tf) {
deltaT = deltaT(tf, ti);
initialVelocity = vf - (a * deltaT);
df.format(initialVelocity);
return initialVelocity;
}当我尝试测试这个方法时,出现了这个问题。我设置了新的替身,并在我的主类中使用getInitialVelocity:
Kinematics test = new Kinematics(); // creates object from inner class
Double vf = 1.0, a = 2.0, ti = 0.5, tf = 1.5;
test.getInitialVelocity(vf, a, ti, tf);当我运行此命令进行测试时,我得到以下错误:
Static Error: No method in Kinematics with name 'getInitialVelocity' matches this invocation
Arguments: (Double, Double, Double, Double)
Candidate signatures: double getInitialVelocity()有没有人知道如何正确地这样做?我需要使用Double类型,因为我正在比较给定给null的值,然后使用基于哪些值为null的适当公式。另外,当从字符串转换时,我应该只使用Double.parseDouble(textField.getText());吗?
编辑1:以下是我的类的相关部分:
私有内部类(运动学):
private class Kinematics {
private Double initialVelocity, finalVelocity, acceleration, timeFinal, timeInitial;
private Double deltaT;
// constructor
public Kinematics() {
}
public Double deltaT(Double tf, Double ti) {
if(!(tf == null && ti == null)){
deltaT = tf - ti;
} return deltaT;
}
public Double getInitialVelocity(Double vf, Double a, Double ti, Double tf) {
deltaT = deltaT(tf, ti);
initialVelocity = vf - (a * deltaT);
df.format(initialVelocity);
return initialVelocity;
}
在我的主类(KinematicsPanel)中,我有:
Kinematics values = new Kinematics();
viLabel = new JLabel("Initial Velocity: ");
viText = new JTextField(1);
vfLabel = new JLabel("Final Velocity: ");
vfText = new JTextField(1);
aLabel = new JLabel("Acceleration: ");
aText = new JTextField(1);
tiLabel = new JLabel("Initial Time: ");
tiText = new JTextField(1);
tfLabel = new JLabel("Final Time: ");
tfText = new JTextField(1);
// compute button & result
compute = new JButton("Compute");
compute.addActionListener(this);
result = new JTextField(2);
result.setEditable(false); // can not be edited
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
// parse each string to a value
Double vf = 0.0, a = 0.0, ti = 0.0, tf = 0.0;
if(vfText != null) {vf = Double.parseDouble(vfText.getText());}
if(aText != null) {a = Double.parseDouble(aText.getText());}
if(tiText != null) {ti = Double.parseDouble(tiText.getText());}
if(tfText != null) {tf = Double.parseDouble(tfText.getText());}
if(action.equals("Compute")) {
if(viText == null) { // get initial velocity
// get values
values.getInitialVelocity(vf, a, ti, tf);
System.out.println(values.toString()); // to test
result.setText(values.toString());
}
}到目前为止,这没有做任何事情,这就是为什么我在Dr.Java的交互窗格中测试该方法的原因。
Edit2:正在使用的格式函数在主类中:
DecimalFormat df = new DecimalFormat("#.00");
发布于 2014-05-15 02:48:21
你的代码没问题,你用的是哪种编译器?方法getInitialVelocity在运动学类中?
发布于 2014-05-15 02:53:50
看起来,您使用的是jdk 1.4或更低版本,那里不支持自动装箱。
所以它不能将双精度转换为双精度。但是这应该会给你一个编译的时间
如果你使用的是像eclipse这样的IDE,那就错了。
或
可能是您调用的方法具有不同的签名。
尝试检查jdk版本并发布整个类,如果上面的版本不能解决您的问题。
https://stackoverflow.com/questions/23662575
复制相似问题