有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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


共 (2) 个答案

  1. # 1 楼答案

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

    所以你需要改变

    if (...) {
    
    }
    if (...) {
    
    }
    if (...) {
    
    }
    

    if (...) {
    
    }
    else if (...) {
    
    }
    else if (...) {
    
    }
    
  2. # 2 楼答案

    你应该使用

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

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