else 在 if 语句中不起作用

Posted

技术标签:

【中文标题】else 在 if 语句中不起作用【英文标题】:else does not work in if statement 【发布时间】:2015-05-12 23:17:03 【问题描述】:

我的 if 语句中的“else”有问题。 如果语句正确,我的 debug-textfield 会得到正确的文本(“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;
    

这是我的日志: 它显示了与我的 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)

【问题讨论】:

你的日志猫说什么???发布它 能否请您发布您的错误消息或logcat? if(a == b) return true; else return false; return a == b; 的路真的很长。 错误信息或日志?? if 语句没有错误。错误是因为Res1。请使用Res1 发布代码。 【参考方案1】:

试试这个对我有用:

   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;
    

【讨论】:

Skizo,这对我有用。谢谢 !但我不明白,为什么 else 在我的例子中不起作用.. @M.Eimire 很高兴它对您有所帮助 :) 如果我解决了您的问题,请将其标记为正确。我也不明白…… 这确实不应该被标记为正确,日志猫表明发布者正在尝试将字符串解析为 int。与给出的示例不同【参考方案2】:

我在上一个答案中编辑了代码:

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!");

【讨论】:

以上是关于else 在 if 语句中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

输入类型单选按钮检查 else 语句在 jquery 中不起作用

fillOval() 在 if 语句中不起作用[关闭]

jQuery if 语句在 for 循环中不起作用

if 语句比较在目标 c 中不起作用的整数

比较 Mongoose 对象 ID 时,if 语句在 Jade 中不起作用

Liquid - if contains 语句在 for 循环中不起作用