我正在为android应用程序编写一些java代码,出于某种原因,即使表达式为false,if语句也正在执行。如果语句运行即使表达式为假
在下面的代码中,if语句if verbLength > 1
即使verbLength为1,if语句仍在运行,它应该执行if语句。我只希望它运行,如果是2个或更多(我也曾尝试verbLength >= 2
)
// Verifies the value of verbLength by showing it in a toast, this displays "1"
Toast.makeText(getApplicationContext(), verbLength, Toast.LENGTH_SHORT).show();
if (verbLength > 1) {
// Lets me verify that this if statement is in fact running
Toast.makeText(getApplicationContext(), "Code executed", Toast.LENGTH_SHORT).show();
// The code that is being executed anyway
TextView cardContent2 = (TextView) findViewById(R.id.verbText2);
cardContent2.setText(verbContentArray[1]);
TextView cardTitle2 = (TextView) findViewById(R.id.verbName2);
cardTitle2.setText(verbTitleArray[1]);
TextView cardPrefix2 = (TextView) findViewById(R.id.verbTitle2);
cardPrefix2.setText(verbPrefix);
}
else{
CardView cardView2 = (CardView) findViewById(R.id.card_view2);
cardView2.setVisibility(View.GONE);
}
的Toast
前if
声明让我知道肯定verbLength = 1,并没有被意外更改。 if语句中的toast显示if
语句已经运行,并且我没有意外地在其他地方运行代码。
我也有另一个if语句,除了它应该只在verbLength >=3
之后才能运行。此代码也正在执行中。
编辑:
如果有帮助,verbLength被声明为前面的代码一个int,并给它的价值通过verbLength = (R.integer.testLength);
如果'verbLength'为int初始化其值的第一个'Toast'应该崩溃您的应用程序 – Blackbelt
@ Blackbelt..It不会 – Renjith
如果verbLength是一个值为1的整数,那么有没有办法应该执行if里面的代码。 尝试在控制语句之前立即设置verbLength = 0的值,以显示控制语句的工作原理。 –