Java 只有在执行语句时才是最后一个

Java 只有在执行语句时才是最后一个,java,android,android-studio,if-statement,Java,Android,Android Studio,If Statement,当我在手机上运行代码时,APK会安装并打开,但当我输入这两个参数时,返回的唯一选项是最终的if语句。即使它是假的,而其他语句是真的,它仍然执行。 这是MainActivity.java文件 float crazy; float hotness; static final String NOGOZONE = "ExampleText"; static final String DANGERZONE = "ExampleText"; static final String FUNZONE =

当我在手机上运行代码时,APK会安装并打开,但当我输入这两个参数时,返回的唯一选项是最终的if语句。即使它是假的,而其他语句是真的,它仍然执行。 这是MainActivity.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);
}
float疯狂;
浮热;
静态最终字符串NOGOZONE=“ExampleText”;
静态最终字符串DANGERZONE=“ExampleText”;
静态最终字符串FUNZONE=“ExampleText”;
静态最终字符串UNICORN=“ExampleText”;
静态最终字符串TRANNY=“ExampleText”;
静态最终字符串LESSTHAN4=“ExampleText”;
静态最终字符串WIFEZONE=“ExampleText”;
静态最终字符串DATEZONE=“ExampleText”;
公用空干管(视图)
{
字符串区域=测试(热,疯狂);
((TextView)findviewbyd(R.id.output)).setText(herZone);
}
静态串测试(浮子热、浮子疯狂)
{
字符串herZone=DANGERZONE;
如果(热度<5)
{
herZone=臭氧;
}
如果(热度>=5&&疯狂=5&&热度<7&&疯狂>5&&疯狂7&&疯狂>8)
{
herZone=DANGERZONE;
}
如果(热度>=7&&hotness<8&&crazy>5&&crazy 7)
{
herZone=DANGERZONE;
}
如果(热度>=8和疯狂>=7)
{
herZone=日期区域;
}
如果(热度>=8&&crazy=8&&crazy=8&&crazy您应该使用

if>else-if语句而不是仅使用if语句


所有IF语句同时执行,因此您只能看到最后一个结果。

如果多个
IF
条件为真,则最后一个将覆盖
herZone
的值

所以你需要改变

if (...) {

}
if (...) {

}
if (...) {

}


我纠正了这一点,现在输出已切换为仅执行第一条语句“if(hotness<5){..}”即使是在false@xhfeyibfds这意味着
hotness
始终小于5。请将TextView文本设置为大于5的值。@xhfeyibfds将
float hotness;
更改为
float hotness=6;
。在尝试学习android之前,请阅读一本关于java的书
if (...) {

}
else if (...) {

}
else if (...) {

}