java – Galaxy S5和Android 5.1.1上的do / while语句中的奇怪行为

前端之家收集整理的这篇文章主要介绍了java – Galaxy S5和Android 5.1.1上的do / while语句中的奇怪行为前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Galaxy S5和 Android 5.1.1上遇到了do / while语句的奇怪行为.如果您有任何信息,请告诉我.
int i = 0;
int j = 0;

do {
    Log.d(TAG,"[test do-while 1] i = " + i + ",j = " + j);
    i = 0;
    Log.d(TAG,"[test do-while 2] i = " + i + ",j = " + j);
    i++;
    Log.d(TAG,"[test do-while 3] i = " + i + ","[test do-while 4] i = " + i + ",j = " + j);
    j++;
    Log.d(TAG,"[test do-while 5] i = " + i + ",j = " + j);
} while (j < 5);

如果我将使用Android 5.1.1在Galaxy S5上运行程序,输出日志如下.

D/MainActivity( 9856): [test do-while 1] i = 2,j = 0
D/MainActivity( 9856): [test do-while 2] i = 2,j = 0
D/MainActivity( 9856): [test do-while 3] i = 2,j = 0
D/MainActivity( 9856): [test do-while 4] i = 2,j = 0
D/MainActivity( 9856): [test do-while 5] i = 2,j = 1
D/MainActivity( 9856): [test do-while 1] i = 2,j = 1
D/MainActivity( 9856): [test do-while 2] i = 2,j = 1
D/MainActivity( 9856): [test do-while 3] i = 2,j = 1
D/MainActivity( 9856): [test do-while 4] i = 2,j = 1
D/MainActivity( 9856): [test do-while 5] i = 2,j = 2
D/MainActivity( 9856): [test do-while 1] i = 2,j = 2
D/MainActivity( 9856): [test do-while 2] i = 2,j = 2
D/MainActivity( 9856): [test do-while 3] i = 2,j = 2
D/MainActivity( 9856): [test do-while 4] i = 2,j = 2
D/MainActivity( 9856): [test do-while 5] i = 2,j = 3
D/MainActivity( 9856): [test do-while 1] i = 2,j = 3
D/MainActivity( 9856): [test do-while 2] i = 2,j = 3
D/MainActivity( 9856): [test do-while 3] i = 2,j = 3
D/MainActivity( 9856): [test do-while 4] i = 2,j = 3
D/MainActivity( 9856): [test do-while 5] i = 2,j = 4
D/MainActivity( 9856): [test do-while 1] i = 2,j = 4
D/MainActivity( 9856): [test do-while 2] i = 2,j = 4
D/MainActivity( 9856): [test do-while 3] i = 2,j = 4
D/MainActivity( 9856): [test do-while 4] i = 2,j = 4
D/MainActivity( 9856): [test do-while 5] i = 2,j = 5

如果我将在其他设备或其他o / s版本上运行相同的程序,输出如下.

D/MainActivity( 9515): [test do-while 1] i = 0,j = 0
D/MainActivity( 9515): [test do-while 2] i = 0,j = 0
D/MainActivity( 9515): [test do-while 3] i = 1,j = 0
D/MainActivity( 9515): [test do-while 4] i = 2,j = 0
D/MainActivity( 9515): [test do-while 5] i = 2,j = 1
D/MainActivity( 9515): [test do-while 1] i = 2,j = 1
D/MainActivity( 9515): [test do-while 2] i = 0,j = 1
D/MainActivity( 9515): [test do-while 3] i = 1,j = 1
D/MainActivity( 9515): [test do-while 4] i = 2,j = 1
D/MainActivity( 9515): [test do-while 5] i = 2,j = 2
D/MainActivity( 9515): [test do-while 1] i = 2,j = 2
D/MainActivity( 9515): [test do-while 2] i = 0,j = 2
D/MainActivity( 9515): [test do-while 3] i = 1,j = 2
D/MainActivity( 9515): [test do-while 4] i = 2,j = 2
D/MainActivity( 9515): [test do-while 5] i = 2,j = 3
D/MainActivity( 9515): [test do-while 1] i = 2,j = 3
D/MainActivity( 9515): [test do-while 2] i = 0,j = 3
D/MainActivity( 9515): [test do-while 3] i = 1,j = 3
D/MainActivity( 9515): [test do-while 4] i = 2,j = 3
D/MainActivity( 9515): [test do-while 5] i = 2,j = 4
D/MainActivity( 9515): [test do-while 1] i = 2,j = 4
D/MainActivity( 9515): [test do-while 2] i = 0,j = 4
D/MainActivity( 9515): [test do-while 3] i = 1,j = 4
D/MainActivity( 9515): [test do-while 4] i = 2,j = 4
D/MainActivity( 9515): [test do-while 5] i = 2,j = 5

>如果我们将代码转换为while或for语句,则不会发生此问题.
>如果我们使用同步文件i / o创建自己的日志库,则会出现问题.
>在Galaxy S4和S6或Android 5.0.2上不会出现.

解决方法

可能不是你正在寻找的答案,但只是一个测试(因为我很好奇):
– 将i声明为final,并为其赋值0.
删除i的增量语句.

如果您得到相同的答案,那么:
你把别的地方宣布为2.
你发现了一个错误.

此外,如果您将i声明为Integer而不是int,是否会出现问题?这里有一些东西需要学习……

原文链接:https://www.f2er.com/android/129181.html

猜你在找的Android相关文章