开发者

I can't get the calculations to work

I have a calculator that is suposed to figure the volume and the results come back as "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;  
            开发者_JAVA百科    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);  
    }


depth is declared as 0 and never overwritten... so the volume is always 0. I guess you should do something like:

...
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);
....


You forgot to convert the strings (lengthString, widthString and depthString) to doubles and assign them to your variables (length, width and depth).


you have depth = 0 and

anything * 0 = 0


you forgot to convert your strings from the input fields to double.

you get 0 as result because you set length and width to 0


In both branches of your main if condition your expressions ending * depth. However this depth variable seems to be set to 0 and not set to anything else. So volume will always be 0 since whatever you multiply by 0 will be 0.

Maybe you've wanted to use depthString. Something like this:

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;  
}  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜