[英]else does not work in if statement
i have a problem with the "else" in my if-statement.我的 if 语句中的“else”有问题。 If the statement is correct, my debug-textfield gets the correct text ("YES,").
如果语句正确,我的调试文本字段就会得到正确的文本(“YES”)。 but if statement is incorrect my app crashes?
但如果声明不正确,我的应用程序会崩溃吗? Could you help me with this please?
你能帮我解决这个问题吗?
Thank you very much in advance!非常感谢您!
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;
}
}
Here's my logcat: It shows a problem with the int "Res1" which has nothing to do with my if statement.这是我的 logcat:它显示了与我的 if 语句无关的 int "Res1" 问题。 Could this be the problem anyways?
无论如何,这可能是问题所在吗? But why does it work, if the statement ist true?!
但是,如果声明是真的,为什么它会起作用?!
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)
Try this it worked to me:试试这个对我有用:
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;
}
}
I edited the code in the previous answer:我编辑了上一个答案中的代码:
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.