gpt4 book ai didi

java - 如何将 TextView 的值放入 if 语句中

转载 作者:行者123 更新时间:2023-12-01 07:33:25 25 4
gpt4 key购买 nike

我正在编写一个 Android 应用程序来计算某人的高度和体重之间的关系。一切都很好,我成功地完成了算术。

但是,当我想将 TextView 的结果放入 if 语句中时,无论结果大于、小于或等于,我都会收到第一个 if 语句的输出。快速地,我希望我的程序根据计算给出三种不同的结果。这是我的代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// My Programming Begins Here
final EditText editTextHeight = (EditText) findViewById(R.id.editTextHeight);
final EditText editTextWeight = (EditText) findViewById(R.id.editTextWeight);
Button calculateButton = (Button) findViewById(R.id.calculateButton);
final TextView result = (TextView) findViewById(R.id.result);
final TextView suggestionResult = (TextView) findViewById(R.id.suggestionResult);
// Coding for the Button
calculateButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
int heightValue = Integer.parseInt(editTextHeight.getText()
.toString());

int weightValue = Integer.parseInt(editTextWeight.getText()
.toString());

// Finding out the square of weightValue and then dividing the
// heightValue by the sqaure of weightValue
result.setText(String.valueOf(heightValue
/ (Math.sqrt(weightValue))));
if (result.getText().toString().length() >= 1
&& result.getText().toString().length() < 18.5) {
suggestionResult.setText("You have to gain weight");
}
if (result.getText().toString().length() >= 18.5
&& result.getText().toString().length() < 24.9) {
suggestionResult.setText("You are normal");
} else {
suggestionResult.setText("You have to lose weight");
}
}
});

}
}

最佳答案

试试这个

double d=Double.parseDouble(result.getText().toString());

if ( d<=18.5) {
suggestionResult.setText("You have to gain weight");
}
if (d >= 18.5 && d < 24.9) {
suggestionResult.setText("You are normal");
} else {
suggestionResult.setText("You have to lose weight");
}

关于java - 如何将 TextView 的值放入 if 语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15365204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com