所以,我正在创建一个GUI,你可以在其中计算摩尔质量,质量或摩尔数,当我调用我的方法时,我似乎无法得到计算结果。我正在使用并行数组,我认为这可能是问题所在。顺便说一下,molarMass在全球宣布,但我不知道如何在这里格式化它。谢谢。
private void btnGoActionPerformed(java.awt.event.ActionEvent evt) {
int indexOfElements = cmbElements.getSelectedIndex();
Double numberOfMass = Double.parseDouble(txtInputMass.getText());
Double numberOfMoles = Double.parseDouble(txtInputMoles.getText());
if (chkMoles.isSelected()==true)
{
txtAnswer.setText(solveForMoles(numberOfMass).toString() + " mol");
}
else if (chkMolarMass.isSelected()==true)
{
txtAnswer.setText(solveForMolarMass(numberOfMass, numberOfMoles).toString() + "
g/mol");
}
else if (chkMass.isSelected()==true)
{
txtAnswer.setText(solveForMass(numberOfMoles).toString() + " g");
}
else
{
txtAnswer.setText ("Please make a selection");
}
private Double solveForMoles (double mass){
int indexOfElements = cmbElements.getSelectedIndex();
double numberOfMoles = mass/molarMass[indexOfElements];
return (Double.parseDouble (x.format(numberOfMoles)));
}
private Double solveForMass (double moles){
int indexOfElements = cmbElements.getSelectedIndex();
double numberOfMass = moles*molarMass[indexOfElements];
return (Double.parseDouble (x.format(numberOfMass)));
}
private Double solveForMolarMass (double masss, double moless){
double numberOfMolarMass = masss/moless;
return (Double.parseDouble(x.format (numberOfMolarMass)));
}
答案 0 :(得分:0)
我建议您进行一些更改: