我已經在一個名爲findCar()的方法中寫了一個循環,它通過鏈接列表汽車並檢查用戶輸入中輸入的Id是否與Car,Car實例Id從Car類的getId()方法中收集。但它總是返回false,有人知道這是爲什麼嗎?您會看到對其他類的引用,例如客戶端和人員,但爲了簡單起見,我省略了它們,但如果具有該代碼將有助於達成答案,我將添加它。任何幫助是極大的讚賞布爾值總是返回false
這裏是根類
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);
}
}
這裏是汽車類
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; }
}
您可以使用調試輸出來檢查您比較的值。例如'System.out.println(chars.size());''和'System.out.println(chars.size(searchid);''和'System.out.println(chars.size(i。 getId());'在正確的位置 – glglgl 2015-04-04 05:52:10
'println'調試是如此古老,調試器終於成爲Java的東西,非常有用,用於檢查狀態和驗證假設.. – user2864740 2015-04-04 06:01:50