gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:33 24 4
gpt4 key购买 nike

我在 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 + ", j = " + j);
i++;
Log.d(TAG, "[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

如果我将在其他设备或其他操作系统版本上运行相同的程序,输出如下。

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 的增量语句。

如果您得到相同的答案,那么:1. 你让我在其他地方声明为 2。2. 你发现了一个错误。

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

关于java - Galaxy S5 和 Android 5.1.1 上 do/while 语句的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31576910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com