java only last IF语句正在执行
当我在手机上运行代码时,APK会安装并打开,但当我输入这两个参数时,返回的唯一选项是最终的if语句。即使它是假的,而其他语句是真的,它仍然执行。 这是主要的活动。java文件
float crazy;
float hotness;
static final String NOGOZONE = "ExampleText";
static final String DANGERZONE = "ExampleText";
static final String FUNZONE = "ExampleText" ;
static final String UNICORN = "ExampleText" ;
static final String TRANNY = "ExampleText" ;
static final String LESSTHAN4 = "ExampleText" ;
static final String WIFEZONE = "ExampleText" ;
static final String DATEZONE = "ExampleText" ;
public void main(View view)
{
String herZone = test(hotness, crazy);
((TextView)findViewById(R.id.output)).setText(herZone);
}
static String test(float hotness, float crazy)
{
String herZone = DANGERZONE;
if ( hotness < 5 )
{
herZone = NOGOZONE;
}
if ( hotness >= 5 && crazy <= 5 )
{
herZone = FUNZONE;
}
if ( hotness >= 5 && hotness < 7 && crazy > 5 && crazy <= 7)
{
herZone = DANGERZONE;
}
if ( hotness > 7 && crazy > 8 )
{
herZone = DANGERZONE;
}
if ( hotness >= 7 && hotness < 8 && crazy > 5 && crazy <= 8)
{
herZone = FUNZONE;
}
if ( hotness < 7 && crazy > 7 )
{
herZone = DANGERZONE;
}
if ( hotness >= 8 && crazy >= 7 )
{
herZone = DATEZONE;
}
if ( hotness >= 8 && crazy <= 7 )
{
herZone = WIFEZONE;
}
if ( hotness >= 8 && crazy <= 5 )
{
herZone = UNICORN;
}
if ( hotness >= 8 && crazy <= 4 )
{
herZone = TRANNY;
}
if ( crazy < 4 )
{
herZone = LESSTHAN4;
}
return herZone;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
这个按钮xml onClick代码是安卓:onClick="main"
。有人知道如何解决这个问题吗?我犯了愚蠢的错误吗?提前感谢:)<;3
# 1 楼答案
如果多个
if
条件为真,则最后一个条件将覆盖herZone
的值所以你需要改变
到
# 2 楼答案
你应该使用
if>;else if语句而不是仅使用if语句
所有IF语句都同时执行,因此您只能看到最后一个结果