Java 我是否正确地转换了这个伪代码?
我只是想在提交我的作品之前,确保我在下面写的代码被正确翻译。这个方法确实有效,但我觉得我可能写错了什么 伪代码:Java 我是否正确地转换了这个伪代码?,java,loops,methods,parameters,Java,Loops,Methods,Parameters,我只是想在提交我的作品之前,确保我在下面写的代码被正确翻译。这个方法确实有效,但我觉得我可能写错了什么 伪代码: assign i the value 0 WHILE i is less than the length of the array minus 1 let bubbleI be the bubble at index i in the bubbles array assign j the value i + 1 WHILE bubbleI is not popped AND
assign i the value 0
WHILE i is less than the length of the array minus 1
let bubbleI be the bubble at index i in the bubbles array
assign j the value i + 1
WHILE bubbleI is not popped AND j is less than the length of the bubbles
array
let bubbleJ be the bubble at index j in the bubbles array
IF bubbleJ is not popped AND bubbleI is touching bubbleJ
pop both bubbleI and bubbleJ
END IF
increment j by 1
END WHILE
increment i by 1
END WHILE
private void popAll() {
int i = 0;
while (i < bubbles.length - 1){
bubbles[i] = bubbles[i];
int j = i + 1;
while (bubbles[i].isPopped() == false && j < bubbles.length){
bubbles[j] = bubbles[j];
if (bubbles[j].isPopped() == false && bubbles[i].isTouching(bubbles[j]) == true){
bubbles[i].pop();
bubbles[j].pop();
}
j++;
}
i++;
}
}
我的代码:
assign i the value 0
WHILE i is less than the length of the array minus 1
let bubbleI be the bubble at index i in the bubbles array
assign j the value i + 1
WHILE bubbleI is not popped AND j is less than the length of the bubbles
array
let bubbleJ be the bubble at index j in the bubbles array
IF bubbleJ is not popped AND bubbleI is touching bubbleJ
pop both bubbleI and bubbleJ
END IF
increment j by 1
END WHILE
increment i by 1
END WHILE
private void popAll() {
int i = 0;
while (i < bubbles.length - 1){
bubbles[i] = bubbles[i];
int j = i + 1;
while (bubbles[i].isPopped() == false && j < bubbles.length){
bubbles[j] = bubbles[j];
if (bubbles[j].isPopped() == false && bubbles[i].isTouching(bubbles[j]) == true){
bubbles[i].pop();
bubbles[j].pop();
}
j++;
}
i++;
}
}
private void popAll(){
int i=0;
而(i<1.length-1){
气泡[i]=气泡[i];
int j=i+1;
while(气泡[i].isPopped()==false&&j
我认为“让bubbleI成为气泡数组中索引I处的气泡”应该变成bubbleI=bubbles[I]代码>,而不是实际上什么都不做的赋值
在if语句中比较true
和false
也是不寻常的-foo==true
与foo
完全相同,foo==false
与完全相同!foo
最后,带有初始化和增量的while循环正是for
语句的目的,因此我将这样写:
private void popAll() {
for (int i = 0; i < bubbles.length - 1; i++) {
Bubble bubbleI = bubbles[i];
for (int j = i + 1; !bubbleI.isPopped() && j < bubbles.length; j++) {
Bubble bubbleJ = bubbles[j];
if (!bubbleJ.isPopped() && bubbleI.isTouching(bubbleJ)) {
bubbleI.pop();
bubbleJ.pop();
}
}
}
}
private void popAll(){
对于(int i=0;i
或者,您可以保留while循环。。现在还不清楚是希望您逐字翻译伪代码,还是尝试编写惯用代码。我认为“让bubbleI成为气泡数组中索引I处的气泡”应该变成bubbleI=bubbles[I]代码>,而不是实际上什么都不做的赋值
在if语句中比较true
和false
也是不寻常的-foo==true
与foo
完全相同,foo==false
与完全相同!foo
最后,带有初始化和增量的while循环正是for
语句的目的,因此我将这样写:
private void popAll() {
for (int i = 0; i < bubbles.length - 1; i++) {
Bubble bubbleI = bubbles[i];
for (int j = i + 1; !bubbleI.isPopped() && j < bubbles.length; j++) {
Bubble bubbleJ = bubbles[j];
if (!bubbleJ.isPopped() && bubbleI.isTouching(bubbleJ)) {
bubbleI.pop();
bubbleJ.pop();
}
}
}
}
private void popAll(){
对于(int i=0;i
或者,您可以保留while循环。。现在还不清楚是希望您逐字翻译伪代码,还是希望您尝试编写惯用代码