Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

java - 算术异常除以零:但是我已经在限制它不产生零(Arithmetic Exception divide by zero : but i am already restricting it not to produce zero)

here is my sample code, logcat is showing exception in while statement, I am out of mind now.

(这是我的示例代码,logcat在while语句中显示异常,我现在不介意。)

I don't know how and why it is being divided by zero.

(我不知道如何以及为什么将其除以零。)

Help me please :

(请帮帮我 :)

public void divisionQuestion() {
    int a;
    int b;
    Random random = new Random();
    do {
        a = random.nextInt(40);
        b = random.nextInt(20);
    } while (a % b != 0 || a < 3 || b < 2);
}

My logcat :

(我的logcat:)

java.lang.IllegalStateException: Could not execute method for android:onClick
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
    at android.view.View.performClick(View.java:6256) 
    at android.view.View$PerformClick.run(View.java:24701) 
    at android.os.Handler.handleCallback(Handler.java:789) 
    at android.os.Handler.dispatchMessage(Handler.java:98) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
 Caused by: java.lang.ArithmeticException: divide by zero
    at com.universeCoding.mathtestapp.QuestionAnswerAdapter.divisionQuestion(Adapter.java:228)
  ask by gagangamer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This statement:

(这个说法:)

a % b != 0

will give an error when b is zero.

(当b为零时将给出错误。)

The problem is that it can be zero, since this line:

(问题是它可以为零,因为此行:)

b = random.nextInt(20);

will allow b to be any random value in the range [0, 20[ .

(将允许b[0, 20[ 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...