簡體   English   中英

else 在 if 語句中不起作用

[英]else does not work in if statement

我的 if 語句中的“else”有問題。 如果語句正確,我的調試文本字段就會得到正確的文本(“YES”)。 但如果聲明不正確,我的應用程序會崩潰嗎? 你能幫我解決這個問題嗎?

非常感謝您!

 int a = 1;
 int b = 1;
 if (isCorrect(a,b)) {
    debug.setText("YES!");
    } else{
      debug.setText("NO!");

    }

public static boolean isCorrect(int a, int b){

    if(a == b) {
        return true;
    }else {
        return false;
    }
}

這是我的 logcat:它顯示了與我的 if 語句無關的 int "Res1" 問題。 無論如何,這可能是問題所在嗎? 但是,如果聲明是真的,為什么它會起作用?!

   03-11 03:44:54.645    2164-2164/de.test.michael.maths D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-11 03:44:54.675    2164-2164/de.test.michael.maths I/System.out﹕ Sending WAIT chunk
03-11 03:44:54.675    2164-2170/de.test.michael.maths E/jdwp﹕ Failed sending reply to debugger: Broken pipe
03-11 03:44:54.675    2164-2170/de.test.michael.maths D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
03-11 03:44:54.675    2164-2164/de.test.michael.maths W/ActivityThread﹕ Application de.test.michael.maths is waiting for the debugger on port 8100...
03-11 03:44:55.865    2164-2170/de.test.michael.maths I/dalvikvm﹕ Debugger is active
03-11 03:44:55.935    2164-2164/de.test.michael.maths I/System.out﹕ Debugger has connected
03-11 03:44:55.935    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.145    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.355    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.565    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.775    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:56.985    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:57.195    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:57.405    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:57.615    2164-2164/de.test.michael.maths I/System.out﹕ waiting for debugger to settle...
03-11 03:44:57.825    2164-2164/de.test.michael.maths I/System.out﹕ debugger has settled (1324)
03-11 03:44:57.865    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
03-11 03:44:57.865    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 11345: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-11 03:44:57.875    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
03-11 03:44:57.875    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 11351: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-11 03:44:57.875    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
03-11 03:44:57.875    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 9039: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
03-11 03:44:57.875    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
03-11 03:44:57.875    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-11 03:44:57.875    2164-2164/de.test.michael.maths I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
03-11 03:44:57.875    2164-2164/de.test.michael.maths W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
03-11 03:44:57.875    2164-2164/de.test.michael.maths D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-11 03:44:57.955    2164-2164/de.test.michael.maths D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
03-11 03:44:57.955    2164-2164/de.test.michael.maths D/﹕ HostConnection::get() New Host Connection established 0xb89f66a0, tid 2164
03-11 03:44:57.965    2164-2164/de.test.michael.maths D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-11 03:44:57.975    2164-2164/de.test.michael.maths D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
03-11 03:44:58.025    2164-2164/de.test.michael.maths W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-11 03:44:58.025    2164-2164/de.test.michael.maths D/OpenGLRenderer﹕ Enabling debug mode 0
03-11 03:45:13.395    2164-2164/de.test.michael.maths D/AndroidRuntime﹕ Shutting down VM
03-11 03:45:13.395    2164-2164/de.test.michael.maths W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0ef3648)
03-11 03:45:13.395    2164-2164/de.test.michael.maths E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid int: "Res1"
        at java.lang.Integer.invalidInt(Integer.java:138)
        at java.lang.Integer.parse(Integer.java:375)
        at java.lang.Integer.parseInt(Integer.java:366)
        at java.lang.Integer.parseInt(Integer.java:332)
        at de.test.michael.maths.MainActivity$1.onClick(MainActivity.java:96)
        at android.view.View.performClick(View.java:4240)
        at android.view.View$PerformClick.run(View.java:17721)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

試試這個對我有用:

   public class HelloWorld {

    private static int a = 1;
    private static int b = 1;
    public static void main(String[] args) {
        if (isCorrect(a, b)) {
            debug.setText("YES!");
        }
        if (!isCorrect(a, b)) {
            debug.setText("NO!");
        }
    }

    public static boolean isCorrect(int a, int b) {
        boolean ok = false;

        if (a == b) {
            ok = true;
        }
        return ok;
    }
}

我編輯了上一個答案中的代碼:

public class HelloWorld {

private static int a = 1;
private static int b = 1;
public static void main(String[] args) {
    debug.setText(a == b? "YES!" : "NO!");
}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM