简单的if语句不能正常工作

Nat*_*Cho 0 java android if-statement

    int sch = 1;

    if (sch == 1){
        Log.d("check", "1");
    }
    if (sch == 2){
        Log.d("check", "2");
    }
    else {
        Log.d("check", "error!");
    }
Run Code Online (Sandbox Code Playgroud)

这是一个非常简单的陈述.

我在我的android java代码中使用完全相同的语句,

check: 1在logcat中显示为假设.

当我得到我正在使用的真实代码时,

    int sch = 1;

    if (sch == 1){
        activity.getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.white));
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            activity.getWindow().setNavigationBarColor(getResources().getColor(R.color.white));
            activity.getWindow().setStatusBarColor(getResources().getColor(R.color.red));}

        Log.d("check", "1");
    }

    if (sch == 2){            
        activity.getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.white));
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            activity.getWindow().setNavigationBarColor(getResources().getColor(R.color.white));
            activity.getWindow().setStatusBarColor(getResources().getColor(R.color.green));}

        Log.d("check", "2");
    }
    else {
        activity.getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.white));
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            activity.getWindow().setNavigationBarColor(getResources().getColor(R.color.white));
            activity.getWindow().setStatusBarColor(getResources().getColor(R.color.white));}

        Log.d("check", "error");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,它应显示check: 1在logcat上并在状态栏上显示红色.

但它显示如下:

check: 1
check: error
Run Code Online (Sandbox Code Playgroud)

并在状态栏上显示白色.

这是确切的代码,我没有发现任何错误.

可能是什么问题?

Com*_*are 6

更换:

if (sch == 2)
Run Code Online (Sandbox Code Playgroud)

有:

else if (sch == 2)
Run Code Online (Sandbox Code Playgroud)

既然这样,对sch1,它将匹配sch==1你的第一个ifelse第二个条件if.有关工作原理的更多详细信息,请参阅此官方Java教程页面else if.