我有一个方法返回一个平面的LinkedList中的所有人的名字.
然而,即使方法中有一个return语句,我仍然被告知有一个缺少的return语句.
如何在不添加其他退货声明的情况下解决这个问题?为什么它不被视为有效?是否将另一个return语句更改为返回的内容?
任何反馈都非常感谢.
public String check() {
for (Person person: passengers)
{
return person.getName();
}
}
Run Code Online (Sandbox Code Playgroud) 我是编程新手,我遇到了一些麻烦.基本上我要做的是让findCar方法循环通过名为cars的LinkedList并让它打印出汽车对象的ID.它会编译但是没有打印出来,有人可以解释一下为什么会这样吗?
这是主要课程
import java.io.*;
import java.util.*;
public class CarManger {
private LinkedList<Car> cars = new LinkedList<Car>();
public void setup()
{
cars.add(new Car(1));
cars.add(new Car(2));
cars.add(new Car(3));
}
public void main() {
char choice;
while ((choice = readChoice()) !='x' ) {
switch(choice) {
case 'a': findCar(); break;
}
}
}
private char readChoice() {
System.out.print("Your choice: ");
return In.nextChar();
}
public void findCar()
{
for (Car i: cars)
{
int value = i.getId();
System.out.println(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是汽车对象
public class Car …
Run Code Online (Sandbox Code Playgroud)