JAVA 除法运算问题.
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 除法运算问题.相关的知识,希望对你有一定的参考价值。
import javax.swing.JOptionPane;
public class CalculateBodyMassIndex
// Main method
public static void main(String[] args)
// Enter a weight in kg
String tempString = JOptionPane.showInputDialog(null,
"Enter a weight in kg", "Input Dialog Demo",
JOptionPane.QUESTION_MESSAGE);
// Enter a height in m
String s=(String)JOptionPane.showInputDialog(null,
"Enter a height in m", "Input Dialog Demo",
JOptionPane.QUESTION_MESSAGE);
// Convert the input string into a double value
double weight = Double.parseDouble(tempString);
double height = Double.parseDouble(tempString);
// Convert it to BMI
double BMI = (weight/(height*height)) ;
// Display the result
JOptionPane.showMessageDialog(null, "The BMI is " +
BMI + " in kg/m2");
=============================================
不知道哪里出错. 运算的结果不对..
程序是要算BMI. 公式是重量/高度^2
感觉是DOUBLE的问题. 可是JAVA水平有限...
不知道哪位大神能帮我解决一下? 感激.~!~
import javax.swing.JOptionPane;
public class CalculateBodyMassIndex
public static void main(String[] args)
String tempString = JOptionPane.showInputDialog(null, "Enter a weight in kg", "Input Dialog Demo", JOptionPane.QUESTION_MESSAGE);
// Enter a height in m
String s=(String)JOptionPane.showInputDialog(null, "Enter a height in m", "Input Dialog Demo", JOptionPane.QUESTION_MESSAGE);
// Convert the input string into a double value
double weight = Double.parseDouble(tempString);
//double height= Double.parseDouble(tempString);中的变量tempString写错了,改成s
double height = Double.parseDouble(s);
// Convert it to BMI
double BMI = (weight/(height*height)) ;
// Display the result
JOptionPane.showMessageDialog(null, "The BMI is " +BMI + " in kg/m2");
来自:求助得到的回答 参考技术A 一个错误自己找
import javax.swing.JOptionPane;
public class CalculateBodyMassIndex
// Main method
public static void main(String[] args)
// Enter a weight in kg
String tempString = JOptionPane.showInputDialog(null,
"Enter a weight in kg", "Input Dialog Demo",
JOptionPane.QUESTION_MESSAGE);
// Enter a height in m
String s = (String) JOptionPane.showInputDialog(null,
"Enter a height in m", "Input Dialog Demo",
JOptionPane.QUESTION_MESSAGE);
// Convert the input string into a double value
double weight = Double.parseDouble(tempString);
//这里你开始转换错了转换成tempString了,应该是s
double height = Double.parseDouble(s);
// Convert it to BMI
double BMI = (weight / (height * height));
// Display the result
JOptionPane.showMessageDialog(null, "The BMI is " + BMI + " in kg/m2");
本回答被提问者和网友采纳 参考技术B 亲,你这个height和weight 是同一个字符串parse的啊,呵呵,还有变量的命名,尽量让自己能清楚是什么东西的名字,tempString,没人会懂什么意思啊?还有教你用一个数据类型,bigdecimal,对于除法结果的精度会优于double,有问题再找我,一起探讨吧。^ ^
import java.math.BigDecimal;
import javax.swing.JOptionPane;
public class CalculateBodyMassIndex
// Main method
public static void main(String[] args)
// Enter a weight in kg
String Weight = JOptionPane.showInputDialog(null,
"Enter a weight in kg", "Input Dialog Demo",
JOptionPane.QUESTION_MESSAGE);
// Enter a height in m
String Height = (String) JOptionPane.showInputDialog(null,
"Enter a height in m", "Input Dialog Demo",
JOptionPane.QUESTION_MESSAGE);
// Convert the input string into a double value
double weight = Double.parseDouble(Weight);
double height = Double.parseDouble(Height);
// Convert it to BMI
BigDecimal BMI = new BigDecimal(weight / (height * height));
JOptionPane.showMessageDialog(null, "The BMI is " + BMI + " in kg/m2");
参考技术C double weight = Double.parseDouble(tempString);
double height = Double.parseDouble(tempString);
看看 第二句代码,第一个是weight,第二个应该是height,而你去还是去的是weight的值 ,所以结果错了 参考技术D 程序是没问题的
我觉得是不是把变量写错了
double height = Double.parseDouble(tempString);
应该写成
double height = Double.parseDouble(s);
以上是关于JAVA 除法运算问题.的主要内容,如果未能解决你的问题,请参考以下文章