我无法计算工作

问题描述:

我有一个suposed推测体积计算器和结果回来为“0”我无法计算工作

JButton btnCalculateVlmn = new JButton("Calculate Hot Tub Volume"); 
     btnCalculateVlmn.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent arg0) 
      { 
       double width = 0, length = 0, depth = 0, volume = 0; 
       String lengthString, widthString, depthString; 
       lengthString = hotTubLengthText.getText(); 
       widthString = hotTubWidthText.getText(); 
       depthString = hotTubDepthText.getText(); 
       try 
       { 
        if (rdbtnRoundTub.isSelected()) 
        { 
         volume = Math.PI * Math.pow(length/2.0, 2) * depth; 
        } 
        else 
        { 
         volume = Math.PI * Math.pow(length * width, 2) 
           * depth; 
        } 
        DecimalFormat formatter = new DecimalFormat("#,###,###.###"); 
        hotTubVolumeText.setText("" + formatter.format(volume)); 
       } 
       catch (NumberFormatException e) 
       { 
        labelTubStatus 
          .setText("Fill in all fields"); 
       } 
      } 
     }); 
     btnCalculateVlmn.setBounds(20, 200, 180, 20); 
     hotTubs.add(btnCalculateVlmn); 
     JButton Exit = new JButton("Exit"); 
     Exit.setBounds(220, 200, 80, 20); 
     Exit.addActionListener(this); 
     hotTubs.add(Exit); 
    } 

深度被声明为0,从来没有被覆盖......所以体积始终是0 我想你应该这样做:

... 
double width = 0, length = 0, depth = 0, volume = 0; 
String lengthString, widthString, depthString; 
lengthString = hotTubLengthText.getText(); 
widthString = hotTubWidthText.getText(); 
depthString = hotTubDepthText.getText(); 
depth = Double.valueOf(depthString); 
length = Double.valueOf(lengthString); 
width = Double.valueOf(widthString); 
.... 
+2

和长度和宽度相同。 – Douglas 2011-03-30 13:15:28

+0

@Douglas:我不确定我是否做得对。 [code]双倍宽度,长度,深度,音量; length = Double.valueOf(toString()); width = Double.valueOf(toString()); depth = Double.valueOf(toString());字符串lengthString,widthString,depthString; lengthString = hotTubLengthText.getText(); widthString = hotTubWidthText.getText(); depthString = hotTubDepthText.getText(); [/ code] – Mike 2011-03-30 13:32:06

+0

@Douglas:你如何在注释中对代码进行格式化,看起来像上面那样?我不认为我的通过是正确的。 – Mike 2011-03-30 13:33:33

你忘了字符串(lengthStringwidthStringdepthString)并将其转换为双打和分配给您的变量(lengthwidthdepth)。

你有depth = 0

anything * 0 = 0 

你忘了你的字符串从输入字段转换翻番。

你得到0的结果,因为你设置的长度和宽度为0

在你的主要if条件两个分支你的表情结束* depth。然而,这个depth变量似乎被设置为0,并没有设置为其他任何东西。所以量将永远是0不管你用0乘以因为将为0

也许你想使用depthString。像这样:

depth = Integer.parseInt(depthString); 
if (rdbtnRoundTub.isSelected()) 
{ 
    volume = Math.PI * Math.pow(length/2.0, 2) * depth; 
} 
else 
{ 
    volume = Math.PI * Math.pow(length * width, 2) * depth; 
}