提交 2efafbc7 编写于 作者: M Mars Liu

add find

上级 a0678c6b
......@@ -57,7 +57,7 @@ public void process() {
process()
System.out.println(x)
System.out.println(x);
```
......@@ -65,12 +65,12 @@ System.out.println(x)
```java
x = 100;
int x = 100;
for(int i = 0; i < 10 ; i++){
x = x + 5;
}
System.out.println(x)
System.out.println(x);
```
\ No newline at end of file
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [],
"keywords": ["大于", "小于", "等于", "比较", "不等于"],
"children": [],
"export": []
"export": ["find.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "find.md",
"exercise_id":"61cca0fdecc34b00b173f4edc575ce48"
}
\ No newline at end of file
# 查找
定义一个函数,查找给定值x在数组 array 中的索引位置,如果没有找到,返回-1 。能够在数组中找到
x 的正确位置的选项是:
## 答案
```java
public int findIn(int[] array, int x){
for(int i=0; i< array.length; i++){
if(array[i] == x){
return i;
}
}
return -1;
}
```
## 选项
### 错用了赋值运算符
```java
public int findIn(int[] array, int x){
for(int i=0; i< array.length; i++){
if(array[i] = x){
return i;
}
}
return -1;
}
```
### 逻辑分支错误
```java
public int findIn(int[] array, int x){
for(int i=0; i< array.length; i++){
if(array[i] == x){
return i;
} else {
return -1;
}
}
}
```
### 错用比较运算符赋值
```java
public int findIn(int[] array, int x){
int index = -1;
for(int i=0; i< array.length; i++){
if(array[i] == x){
index == x;
}
}
return index;
}
```
### 备选
以上都不是
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册