提交 808920cb 编写于 作者: M Mars Liu

add max and find

上级 2efafbc7
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
"node_id": "569d5e11c4fc5de7844053d9a733c5e8", "node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": ["大于", "小于", "等于", "比较", "不等于"], "keywords": ["大于", "小于", "等于", "比较", "不等于"],
"children": [], "children": [],
"export": ["find.json"] "export": ["find.json", "max.json"]
} }
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "max.md",
"exercise_id":"5025886554e5470e82710ed84735e419"
}
\ No newline at end of file
# 查找最大值
下面哪个函数能够从数组中找到最大值(如果数组为空,返回 0)?
## aop
### before
```java
int[] array = new int[]{1, 28766, 3, 4, 75, 32421, 12, 3232, 932};
```
### after
## 答案
```java
int max(int[] array){
int result = 0;
for(int i=0;i<array.length;i++){
int value = array[i];
if(value > result){
result = value;
}
}
return value;
}
```
## 选项
### 符号用反
```java
int max(int[] array){
int result = 0;
for(int i=0;i<array.length;i++){
int value = array[i];
if(value < result){
result = value;
}
}
return result;
}
```
### final 无法修改
```java
int max(int[] array){
final int result = 0;
for(int i=0;i<array.length;i++){
int value = array[i];
if(value > result){
result = value;
}
}
return result;
}
```
### 错用比较运算符
```java
int max(int[] array){
final int result = 0;
for(int i=0;i<array.length;i++){
int value = array[i];
if(value > result){
result == value;
}
}
return result;
}
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册