Boolean始终返回false

时间:2015-04-04 05:39:42

标签: java linked-list

我在一个名为findCar()的方法中编写了一个循环,该循环遍历链接列表汽车并检查从用户输入输入的Id是否与Car实例中的Id相同,Car实例标识从Car类中的getId()方法收集。然而它总是返回假,有谁知道为什么这是?您将看到对其他类的引用,例如客户端和人员,但是为了简单起见我省略了它们,但是如果具有该代码将有助于达到答案,我将添加它。非常感谢任何帮助

这是Root Class

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 class

 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;   }
}

1 个答案:

答案 0 :(得分:2)

问题是你从不调用CarManager.setUp()方法,这意味着linkedlist总是空的,因此返回值总是为假。