gpt4 book ai didi

java - 在 While 循环中转换 do While

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:58 26 4
gpt4 key购买 nike

我的任务遇到问题。我必须将以下 doWhile 循环转换为 While 循环,以便两个循环的结果相同?正如您所看到的,Integer 类型就在 doWhile 循环中。但是 int 类型有什么区别呢?我该如何解决这个任务?

doWhile 循环:

public Integer a_doWhile(int x){
int i = 0;
Integer result;
do {
result = ++i*i;
} while (result < x);
return result;
}

我的解决方案:

public Integer a_while(int x){
int i=0;
int result;
while(result < x){
result = ++i*i;
return result;
}
}

但是我的解决方案是错误的。它与这个“整数”有关,有人可以帮助我吗?谢谢:)

最佳答案

int 和 Integer 是同一事物的不同表示。 Int 是原始类型,而 Integer 是对象表示形式。此处讨论 What is the difference between an int and an Integer in Java and C#?

你可以像这样将 dowhile 转换为 while

public Integer a_While(int x){
int i = 0;
Integer result = ++i*i;
while (result < x) {
result = ++i*i;
}
return result;

关于java - 在 While 循环中转换 do While,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40708803/

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