如何将变量FTDEE传递到另一个活动中?我似乎有问题,因为在IF-ELSE语句中声明了FTDEE,但我认为我实际上并没有从IF-ELSE语句之外访问此变量FTDEE。
final float fAF;
float fTdee;
String strAF = tvAF.getText().toString();
if (strAF.matches("OptionA"))
{
fAF = 1.20f;
fTdee = bmr*fAF;
}
else if (strAF.matches("OptionB"))
{
fAF = 1.375f;
fTdee = bmr*fAF;
}
else if (strAF.matches("OptionC"))
{
fAF = 1.55f;
fTdee = bmr*fAF;
}
else if (strAF.matches("OptionD"))
{
fAF = 1.725f;
fTdee = bmr*fAF;
}
else if (strAF.matches("OptionE"))
{
fAF = 1.90f;
fTdee = bmr*fAF;
}
Intent in2 = new Intent(getApplicationContext(), CaloriesPage.class);
in2.putExtra("mTdee",fTdee);
startActivity(in2);
尝试类似的东西
float fAF;
float fTdee;
String strAF = tvAF.getText().toString();
if (strAF.matches("OptionA"))
{
fAF = 1.20f;
fTdee = bmr*fAF;
}
在您的语句之外声明 fTdee
。和不让它们final
,这意味着您无法在Instanciation后修改它们。
float fAF = 0.0f;
String strAF = tvAF.getText().toString();
if (strAF.matches("OptionA")){
fAF = 1.20f;
}else if (strAF.matches("OptionB")){
fAF = 1.375f;
}else if (strAF.matches("OptionC")){
fAF = 1.55f;
}else if (strAF.matches("OptionD")){
fAF = 1.725f;
}else if (strAF.matches("OptionE")){
fAF = 1.90f;
}
final float fTdee = bmr*fAF;
Intent in2 = new Intent(getApplicationContext(), CaloriesPage.class);
in2.putExtra("mTdee",fTdee);
startActivity(in2);
protip:使用更好的var名称
如果其他障碍,请尝试初始化上面的变量。
final float fAF;
String strAF = tvAF.getText().toString();
float fTdee = 0.0;
if (strAF.matches("OptionA"))
{
fAF = 1.20f;
fTdee = bmr*fAF;
}
else if (strAF.matches("OptionB"))
{
fAF = 1.375f;
fTdee = bmr*fAF;
}
else if (strAF.matches("OptionC"))
{
fAF = 1.55f;
fTdee = bmr*fAF;
}
else if (strAF.matches("OptionD"))
{
fAF = 1.725f;
fTdee = bmr*fAF;
}
else if (strAF.matches("OptionE"))
{
fAF = 1.90f;
fTdee = bmr*fAF;
}
Intent in2 = new Intent(getApplicationContext(), CaloriesPage.class);
in2.putExtra("mTdee",fTdee);
startActivity(in2);
您正在编程兄弟。如果您看到一些冗余的东西,则必须自动意识到它可以减少。您已经在每个IF范围中宣布了FTDEE,这在良好的编程方面非常糟糕。相反,如注释中所述,声明活动级范围变量(将其初始化为默认值),并在您想要时设置其值(在IF-ELSE语句中)。
final float fAF;
float fTdee;
String strAF = tvAF.getText().toString();
if (strAF.matches("OptionA"))
{
fAF = 1.20f;
fTdee = bmr*fAF;
}
等等。
仅在if语句中可见变量ftdee。可能是此链接可能有用http://www.java-made-easy.com/variable-scope.html
在声明ftdee时声明意图对象,然后将ftdee值放在if-else语句中。 Float FAF; float ftdee; 字符串straf = tvaf.getText()。toString(); intent in2 = new Intent(getApplicationContext(),caloriespage.class);
if (strAF.matches("OptionA")){
fAF = 1.20f;
fTdee = bmr*fAF;
in2.putExtra("mTdee",fTdee);
}else if (strAF.matches("OptionB")){
fAF = 1.375f;
fTdee = bmr*fAF;
in2.putExtra("mTdee",fTdee);
}else if (strAF.matches("OptionC")){
fAF = 1.55f;
fTdee = bmr*fAF;
in2.putExtra("mTdee",fTdee);
}else if (strAF.matches("OptionD")){
fAF = 1.725f;
fTdee = bmr*fAF;
in2.putExtra("mTdee",fTdee);
}else if (strAF.matches("OptionE")){
fAF = 1.90f;
fTdee = bmr*fAF;
in2.putExtra("mTdee",fTdee);
}
startActivity(in2);
Postscript:如果您看到上述灵魂,您正在写作 ftdee = bmr*faf; in2.putextra(" mtdee",ftdee);一次又一次地在If-Else块中,这是重生。因此,为了避免这种情况
float fAF=1.0f;
float fTdee;
String strAF = tvAF.getText().toString();
if (strAF.matches("OptionA")){
fAF = 1.20f;
}else if (strAF.matches("OptionB")){
fAF = 1.375f;
}else if (strAF.matches("OptionC")){
fAF = 1.55f;
}else if (strAF.matches("OptionD")){
fAF = 1.725f;
}else if (strAF.matches("OptionE")){
fAF = 1.90f;
}
Intent in2 = new Intent(getApplicationContext(), CaloriesPage.class);
fTdee = bmr*fAF;
in2.putExtra("mTdee",fTdee);
startActivity(in2);
在此代码中,当您使用时,您无法将fTdee
初始化。您可以在if-else
语句树的末尾添加其他语句。
final float fAF;
String strAF = tvAF.getText().toString();
float fTdee = 0.0;
if (strAF.matches("OptionA")) {
fAF = 1.20f;
fTdee = bmr * fAF;
} else if (strAF.matches("OptionB")) {
fAF = 1.375f;
fTdee = bmr * fAF;
} else if (strAF.matches("OptionC")) {
fAF = 1.55f;
fTdee = bmr * fAF;
} else if (strAF.matches("OptionD")) {
fAF = 1.725f;
fTdee = bmr * fAF;
} else if (strAF.matches("OptionE")) {
fAF = 1.90f;
fTdee = bmr * fAF;
} else {
fTdee = 0f; //some default value
//or throw IllegalStateException if it is not possible for execution of your code
//throw new IllegalStateException();
}
Intent in2 = new Intent(getApplicationContext(), CaloriesPage.class);
in2.putExtra("mTdee", fTdee);
startActivity(in2);