【问题标题】:double and int in EclipseEclipse 中的 double 和 int
【发布时间】:2011-10-06 22:01:34
【问题描述】:

我是学习 Android 的新手,我忘记了很多 Java 程序。这里有个问题我今天在Eclipse中执行程序。

public void cvOnClick (View cvView) {
    EditText KilogramsInput = (EditText) findViewById(R.id.Kilograms);
    EditText HeightInput = (EditText) findViewById(R.id.Height);
    TextView BmiOutput = (TextView) findViewById(R.id.Bmi);
    DecimalFormat BmiFormat = new DecimalFormat(".#####");
    Double Heightdouble = Math.pow(Integer.parseInt(HeightInput.getText().toString()), 2);          

    String outcome = BmiFormat.format(Integer.parseInt(KilogramsInput.getText().toString())/Heightdouble);
    BmiOutput.setText(outcome);
}

作为这个程序的最初目的,应该是高度的平方数。在Eclipse中执行没有错误。但是当我将数据(十进制)放入 AVD 时,它显示:the application has stopped unexpectedly

请告诉我此刻我能做什么?

感谢您的所有帮助!布拉德

【问题讨论】:

    标签: android double int


    【解决方案1】:

    如果您在公斤或高度编辑文本中输入一个非整数值,您将得到一个异常,因为您正试图将它们解析为整数。使用Double.parseDouble() 而不是Integer.parseInt()

    【讨论】:

      【解决方案2】:

      您正在使用Integer.parseInt(),但是当您输入double 而不是int 时,它会变得混乱。使用Double.parseDouble() 而不是Integer.parseInt(),它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        • 2020-02-19
        • 2014-02-11
        • 2019-01-31
        • 2015-06-08
        • 2013-07-30
        • 2011-08-26
        相关资源
        最近更新 更多