我在名为 findCar() 的方法中编写了一个循环,该循环遍历链接列表 cars 并检查用户输入中输入的 Id 是否与 Car 实例中的 Id 相同,收集 Car 实例 Id来自 Car 类中的 getId() 方法。但它总是返回 false,有人知道这是为什么吗?您将看到对其他类(例如 Clients 和 Person)的引用,但是为了简单起见,我省略了它们,但是如果拥有该代码将有助于达成答案,我将添加它。非常感谢任何帮助
这是根类
import java.io.*;
public class Root
{ public Root() {
new CarManager();
}
public static void main(String[] args)
{ new Root(); }
private CarManager carManager;
}
这里是调用循环的CarManager类,我已经标记了问题 区域//!!
import java.io.*;
import java.util.*;
public class CarManager implements Serializable
{ private LinkedList<Car> cars = new LinkedList<Car>();
private Clients clients = new Clients();
public CarManager(){
menu();
}
// !! Here is where the cars are initialized, the Id is the first number
public void setup()
{ cars.add(new Car(1, "Ed", 3));
cars.add(new Car(2, "Fred", 7));
cars.add(new Car(3, "Freda", 5)); }
public void menu() {
char choice;
while ((choice = readChoice()) !='x' ) {
switch(choice) {
case 'a': clients.makePerson(); break;
case 's': clients.showClients(); break;
case 'r':clients.removeClient(); break;
case 'b': findCar(); break;
default: showMenu(); // break;
}
}
}
private int nextInt() {
return In.nextInt();
}
// !! Here is where the the loop is, it checks the entered Id value and
// !! checks if it matches the Id value of the car objects.
public void findCar() {
System.out.println("Please enter Id of car to be found");
int searchid = In.nextInt();
boolean carfound = false;
for (Car i: cars)
{
if (searchid == i.getId())
{
carfound = true;
System.out.println("found car");}
}
if (carfound == false)
System.out.println("Did not find car");
}
private char readChoice() {
System.out.print("Your choice: ");
return In.nextChar();
}
public void exit() {
System.exit(0);
}
}
这是 Car 类
import java.io.*;
import java.util.*;
public class Car implements Serializable
{
private int id;
private String pilot;
private int stops;
private LinkedList<Person> passengers = new LinkedList<Person>();
private double rate = 10.00;
public int scannableId = this.id;
//
public Car(int id, String pilot, int stops)
{ this.id = id;
this.pilot = pilot;
this.stops = stops; }
private void charge(int i)
{
//this is yet to be finished please ignore
}
private boolean stopAt(int i)
{ for (Person person: passengers)
if (person.uses(i))
return true;
return false; }
private void showStop(int i)
{ String s = " Stop " + i + "\n";
String on = on(i);
if (!on.isEmpty())
s += " On: " + on + "\n";
System.out.print(s); }
private String on(int i)
{ String s = "";
for (Person person: passengers)
if (person.getsOn(i))
s += person.handle() + " ";
return s; }
// !! Here is where the Id value is given from the instance of car
public int getId() {
return this.id;
}
}
这里是处理用户输入的地方
import java.util.*;
public class In
{ private static Scanner in = new Scanner(System.in);
public static String nextLine()
{ return in.nextLine(); }
public static char nextChar()
{ return in.nextLine().charAt(0); }
public static int nextInt()
{ int i = in.nextInt();
in.nextLine();
return i; }
public static double nextDouble()
{ double d = in.nextDouble();
in.nextLine();
return d; }
}
最佳答案
问题是您从不调用 CarManager.setUp()
方法,这意味着链表始终为空,因此返回值始终为 false。
关于java - boolean 值总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29443166/