英文:
How to make this method only have 1 return statement instead of 2
问题
我的教授有一个规则,即“任何方法中不应该有多于一个返回语句”。现在我在我的第一个项目中编写了一个非常简单的方法。如何使这个方法只有一个返回语句而不破坏功能呢?
public static int find(int correctNumber, int numArray[], int lastNum) {
for (int i = 0; i < lastNum; i++) {
if (numArray[i] == correctNumber) {
return i;
}
}
return -1;
英文:
My professor has a rule that "there should be no more than one return statement in any method". Now I've written a pretty simple method in my first project. How would I make this have only return statement without ruining functionality?
public static int find(int correctNumber, int numArray[], int lastNum) {
for (int i = 0; i < lastNum; i++) {
if (numArray[i] == correctNumber) {
return i;
}
}
return -1;
答案1
得分: 2
用另一个变量来赋值,然后在if语句内部使用break来退出循环。
public static int find(int correctNumber, int numArray[], int lastNum) {
int returnValue = -1;
for (int i = 0; i < lastNum; i++) {
if (numArray[i] == correctNumber) {
returnValue = i;
break;
}
}
return returnValue;
}
而且你会发现许多经验丰富的专业程序员认为使用多个return语句是完全可以接受的。
例如:这是JDK HashMap
源代码中的equals重写。该代码是由四位国际认可的计算机科学家编写的,其中三位拥有博士学位。
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
英文:
Use another variable to assign the value and then apply a break within the if clause to exit the loop.
public static int find(int correctNumber, int numArray[], int lastNum) {
int returnValue = -1;
for (int i = 0; i < lastNum; i++) {
if (numArray[i] == correctNumber) {
returnValue = i;
break;
}
}
return returnValue;
}
And you will find that many professional programmers with lots of experience find it perfectly acceptable to use multiple return statements.
For example: This is the equals override from the JDK HashMap
source. That code was written by four internationally recognized computer scientists, three of which have their PhD's.
public final boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
</details>
# 答案2
**得分**: 0
像这样?
```java
public static int find(int correctNumber, int numArray[], int lastNum) {
int correctIndex = -1;
for (int i = 0; i < lastNum; i++) {
if (numArray[i] == correctNumber) {
correctIndex = i;
}
}
return correctIndex;
}
英文:
like this?
public static int find(int correctNumber, int numArray[], int lastNum) {
int correctIndex = -1;
for (int i = 0; i < lastNum; i++) {
if (numArray[i] == correctNumber) {
correctIndex = i;
}
}
return correctIndex;
答案3
得分: 0
你可以尝试这个:
public static int find(int correctNumber, int numArray[], int lastNum) {
int temNum = -1;
for (int i = 0; i < lastNum; i++) {
if (numArray[i] == correctNumber) {
temNum = i;
break;
}
}
return temNum;
}
英文:
you can try this:
public static int find(int correctNumber, int numArray[], int lastNum) {
int temNum = -1;
for (int i = 0; i < lastNum; i++) {
if (numArray[i] == correctNumber) {
temNum = i;
break;
}
}
return temNum;
}
答案4
得分: 0
public static int find(int correctNumber, int numArray[], int lastNum) {
int i = lastNum - 1;
while (i >= 0 && numArray[i] != correctNumber) {
i--;
}
return i;
}
This differs slightly from your code in that, if the 'correctNumber' occurs twice, it will find the higher-index instance.
It could be expressed with a for-loop, but I find I prefer the while-loop.
public static int find(int correctNumber, int numArray[], int lastNum) {
int i;
for (i = lastNum-1; i >= 0 && numArray[i] != correctNumber; i--) {
}
return i;
}
英文:
public static int find(int correctNumber, int numArray[], int lastNum) {
int i = lastNum - 1;
while (i >= 0 && numArray[i] != correctNumber) {
i--;
}
return i;
}
This differs slightly from your code in that, if the 'correctNumber' occurs twice, it will find the higher-index instance.
It could be expressed with a for-loop but I find I prefer the while-loop.
public static int find(int correctNumber, int numArray[], int lastNum) {
int i;
for (i = lastNum-1; i >= 0 && numArray[i] != correctNumber; i--) {
}
return i;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论