gpt4 book ai didi

java - 如何修复 "if"始终变为 "true"

转载 作者:行者123 更新时间:2023-12-01 19:35:37 26 4
gpt4 key购买 nike

我用java编写了一个左旋转脚本,并且在代码中使用了2个for循环。

我使用了 if 条件来检查第二个循环中的第一次迭代,然后在其中进行一些处理。我传递 2 个参数“a”是一个数组,“d”是不旋转。

static int[] rotLeft(int[] a, int d) {
int temp1 = 0;
int temp = 0;
int count = 0;
int j;
for(int i = 0; i < d; i++){
for(j = a.length - 1; j > 0; j--){
if(j == a.length - 1){
temp1 = a[0];
temp = a[j];
a[j] = temp1;
}
else{
temp1 = a[j];
a[j] = temp;
}
}
return a;
}

但问题是每次迭代都会发生。我知道我犯了一些愚蠢的错误。

最佳答案

        else
temp1 = a[j];
a[j] = temp;

我怀疑你的意思是:

        else {
temp1 = a[j];
a[j] = temp;
}

空格在 Java 中没有语义意义(与少数其他语言不同,例如 Python)。

关于java - 如何修复 "if"始终变为 "true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57753843/

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