我的任务有问题。我必须将以下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;
}
}
但我的解决方案是错误的。它必须与这个“整数”一起做,有人可以帮助我吗?谢谢:))
答案 0 :(得分:0)
int和Integer是同一事物的不同表示。 Int是原始的,而Integer是对象表示。这将在此处讨论What is the difference between an int and an Integer in Java and C#?
您可以将dowhile转换为像这样的
public Integer a_While(int x){
int i = 0;
Integer result = ++i*i;
while (result < x) {
result = ++i*i;
}
return result;