gpt4 book ai didi

java - 如何用 while 循环替换 for 循环方法

转载 作者:行者123 更新时间:2023-12-03 21:44:56 24 4
gpt4 key购买 nike

<分区>

我是编程新手,正在尝试学习 Java。我试图用 while 循环替换此方法中的 for 循环。任何人都可以帮助我如何做,因为我编写的代码不正确。

这是原创

public static int doSomething(int [] a, int x) {
for (int i = 0; i < a.length; ++i)
if (a[i]<=x) return i;

return -1;
}

这是我到目前为止所写的,但我一直在得到 i++;无法到达。不确定我到目前为止所写的内容是否正确,但我将不胜感激任何帮助。谢谢

i=0;

while (i < a.length){
if(a[i] <= x) {
return i;
i++;
} else {
return -1;
}
}

return i;

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