public boolean find(a02 p){ for(int i = 0;i < name.length;i++){ if(this.name.equals(p.name[i])){ return true; } } }
时间: 2024-04-17 22:23:19 浏览: 21
在你提供的代码片段中,有一个方法名为find,返回类型为boolean,接受一个参数p,类型为a02。该方法的作用是在数组name中查找是否存在与p.name[i]相等的元素,如果存在则返回true,否则返回false。
然而,你的代码存在一些问题。在方法的结尾处,没有返回值。根据你的逻辑,当找到匹配的元素时应该返回true,但是当没有找到匹配的元素时应该返回false。因此,你需要在方法的最后添加一个默认的返回值,例如return false。修正后的代码如下所示:
public boolean find(a02 p){
for(int i = 0;i < name.length;i++){
if(this.name.equals(p.name[i])){
return true;
}
}
return false; // 添加默认的返回值
}
相关问题
为什么以下代码不能运行private static void printArr(Students[] arr) { for (int i = 0; i < arr.length; i++) { Students stu = arr[i]; if (stu != null) { System.out.println(stu.getId() + "," + stu.getName() + "," + stu.getAge()); } } } private static Students[] creatNewArr(Students[] arr) { Students[] newArr = new Students[arr.length + 1]; for (int i = 0; i < arr.length; i++) { newArr[i] = arr[i]; } return newArr; } private static int getcount(Students[] arr) { int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] != null) ; count++; } return count; } private static boolean contains(Students[] arr, int id) { for (int i = 0; i < arr.length; i++) { //依次或取数组里面的每一个对象 Students stu = arr[i]; //获取学生的id int sid = stu.getId(); //进行比较 if (sid == id) { return true; } } //循环结束 return false; } }
这段代码缺少一个类的定义,其中的方法都是静态方法,可能是在某个类中定义的。如果没有这个类,那么这些方法就无法调用,也就无法运行。同时,getcount方法中的if语句有误,应该是判断是否为null,而不是分号。可以按照如下方式进行修改:
```
public class StudentManager {
private static void printArr(Students[] arr) {
for (int i = 0; i < arr.length; i++) {
Students stu = arr[i];
if (stu != null) {
System.out.println(stu.getId() + "," + stu.getName() + "," + stu.getAge());
}
}
}
private static Students[] creatNewArr(Students[] arr) {
Students[] newArr = new Students[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
private static int getcount(Students[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
count++;
}
}
return count;
}
private static boolean contains(Students[] arr, int id) {
for (int i = 0; i < arr.length; i++) {
Students stu = arr[i];
int sid = stu.getId();
if (sid == id) {
return true;
}
}
return false;
}
}
```
此时,可以在其他类中通过StudentManager.printArr等方法来调用这些静态方法。
create_walls(){ const g = []; for(let r = 0; r < this.rows; r ++ ){ g[r] = []; for(let c = 0; c < this.cows; c ++ ){ g[r][c] = false; } } //四周加墙 for(let r = 0; r < this.rows; r ++ ){ g[r][0] = g[r][this.cols - 1] = true; } for(let c = 0; c < this.cols; c ++ ){ g[0][c] = g[this.rows - 1][c] = true; } //随机化 for(let i = 0; i < this.inner_walls_count; i ++){ for (let j = 0; j < 1000; j ++){ let r = parseInt(Math.random() * this.rows); let c = parseInt(Math.random() * this.cols); if(g[r][c] || r == this.rows - 2 && c == 1 || c == this.cols - 2 && r == 1){ continue; } g[r][c] = g[this.rows - 1 - r][this.cols - 1 -c] = true; break; } } const copy_g = JSON.parse(JSON.stringify(g));//深拷贝 if(!this.check_connectivity(copy_g, this.rows - 2, 1, 1, this.cols - 2)){ return false; } for(let r = 0; r < this.rows; r ++ ){ for(let c = 0; c < this.cols; c ++ ){ if(g[r][c]){ this.walls.push(new Wall(r, c, this)); } } } return true; }翻译成Java
以下是将 JavaScript 代码转换为 Java 代码后的结果:
public boolean createWalls() {
boolean[][] g = new boolean[this.rows][this.cols];
for (int r = 0; r < this.rows; r++) {
for (int c = 0; c < this.cols; c++) {
g[r][c] = false;
}
}
// 四周加墙
for (int r = 0; r < this.rows; r++) {
g[r][0] = g[r][this.cols - 1] = true;
}
for (int c = 0; c < this.cols; c++) {
g[0][c] = g[this.rows - 1][c] = true;
}
// 随机化
for (int i = 0; i < this.inner_walls_count; i++) {
for (int j = 0; j < 1000; j++) {
int r = (int) (Math.random() * this.rows);
int c = (int) (Math.random() * this.cols);
if (g[r][c] || (r == this.rows - 2 && c == 1) || (c == this.cols - 2 && r == 1)) {
continue;
}
g[r][c] = g[this.rows - 1 - r][this.cols - 1 - c] = true;
break;
}
}
boolean[][] copy_g = new boolean[this.rows][this.cols];
for (int r = 0; r < this.rows; r++) {
for (int c = 0; c < this.cols; c++) {
copy_g[r][c] = g[r][c];
}
}
if (!this.checkConnectivity(copy_g, this.rows - 2, 1, 1, this.cols - 2)) {
return false;
}
for (int r = 0; r < this.rows; r++) {
for (int c = 0; c < this.cols; c++) {
if (g[r][c]) {
this.walls.add(new Wall(r, c, this));
}
}
}
return true;
}