我在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上不会出现.