如何将这个方法改为仅有一个返回语句,而不是两个。

huangapple go评论71阅读模式
英文:

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 &lt; 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 &lt; 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&lt;?,?&gt; e = (Map.Entry&lt;?,?&gt;)o;    
        if (Objects.equals(key, e.getKey()) &amp;&amp;   
            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 &lt; 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 &lt; 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 &gt;= 0 &amp;&amp; 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 &gt;= 0 &amp;&amp; numArray[i] != correctNumber; i--) {
    }
    return i;
}

huangapple
  • 本文由 发表于 2020年8月28日 09:56:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63626411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定