ADT中,下面一段Android代码中的if判断句前两个if为什么始终不执行(结果始终"偏旁")?

图片说明

MainActivity.java

 package com.example.a;

import javax.security.auth.PrivateCredentialPermission;

import android.app.Activity;
import android.content.DialogInterface;
//import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;                       /*与"import android.content.DialogInterface.OnClickListener;"冲突*/

public class MainActivity extends Activity {

    EditText shengao,tizhong;
    Button jisuan,quxiao;
    double height=0;
    double weight=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        shengao=(EditText)findViewById(R.id.shengao);
        tizhong=(EditText)findViewById(R.id.tizhong);
        jisuan=(Button)findViewById(R.id.jisuan);
        quxiao=(Button)findViewById(R.id.quxiao);

        jisuan.setOnClickListener(new View.OnClickListener() {  //导入"import android.view.View.OnClickListener;"后无需加"View"

            @Override
            public void onClick(View v) {
                try{
                    if("".equals(shengao.getText().toString()))
                  //if("".equals(shengao.getText().toString().trim()))
                  //if(shengao.getText()==null || "".equals(shengao.getText().toString().trim()))
                    {
                        shengao.setError("身高不得为空");
                    }
                    if("".equals(shengao.getText().toString().trim()))
                    {
                        tizhong.setError("体重不能为空");
                    }

                    if("".equals(shengao.getText().toString()))
                  //if("".equals(shengao.getText().toString().trim()))
                  //if(shengao.getText()!=null && "".equals(shengao.getText().toString().trim()))
                    {
                        height=Double.parseDouble(shengao.getText().toString());
                    }
                    if(tizhong.getText()!=null && "".equals(tizhong.getText().toString().trim()))
                    {
                        weight=Double.parseDouble(tizhong.getText().toString());
                    }

                    //double bmi=weight*10000/height/height;
                    double bmi=weight/(height*height);
                    //Toast.makeText(MainActivity.this, ""+bmi, 10000);
                    if(bmi<18)
                    {
                        Toast.makeText(MainActivity.this, "您身材偏瘦, 请加强营养",3000).show();
                    }
                    else if(bmi<=25)
                    {
                        Toast.makeText(MainActivity.this, "您身材标准, 请继续保持",3000).show();
                    }
                    else
                    {
                        Toast.makeText(MainActivity.this, "您身材偏胖, 请加强锻炼",3000).show();

                    }
                }

                catch(NumberFormatException e)
                {
                    Toast.makeText(MainActivity.this, "身高或体重必须是数字", 3000).show();
                    e.printStackTrace();
                }
            }
        });


        quxiao.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                shengao.setText("");
                tizhong.setText("");
            }
        });

    }

}

fragment_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="身高" />

    <EditText
        android:id="@+id/shengao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="体重" />

    <EditText
        android:id="@+id/tizhong"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <Button
        android:id="@+id/jisuan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算" />

    <Button
        android:id="@+id/quxiao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消" />

</LinearLayout>

height,weight赋值地方的判断条件写反了

if("".equals(shengao.getText().toString())) 这个判断条件前面要加个非吧,不为空的时候,你这是为空的时候执行呀

下面体重的那个不为空判断条件也一样

shengao.getText().toString() 这个获取的是文字身高,不是数据2吧, 改成获取EditText这个文本框中的值

你把数据打印出来自己算算,看看是逻辑错了没

结果是nan不符合前面两个判断条件,自己调试跟代码看看吧

是你下面这段代码的问题吧:
if("".equals(shengao.getText().toString()))
//if("".equals(shengao.getText().toString().trim()))
//if(shengao.getText()!=null && "".equals(shengao.getText().toString().trim()))
{
height=Double.parseDouble(shengao.getText().toString());
}

你把第二和第三个if注释掉了,剩下第一个,"".equals(shengao.getText().toString())这个条件不满足,所以height没有重新赋值,依然是初始化的0,所以最后计算的是8/(0x0),所以偏重啊。所以你应该注释掉第一和第二个,保留第三个if吧。