布尔值总是 returns false
Boolean always returns false
我在一个名为 findCar() 的方法中编写了一个循环,它遍历链表汽车,并检查用户输入的 ID 是否与 Car 实例中的 Id 相同,即 Car 实例 Id从 Car class 中的 getId() 方法收集。但是它总是返回false,有谁知道这是为什么?您会看到对其他 class 的引用,例如 Clients 和 Person,但是为了简单起见,我省略了它们,但是如果拥有该代码有助于获得答案,我会添加它。非常感谢任何帮助
这是根Class
import java.io.*;
public class Root
{ public Root() {
new CarManager();
}
public static void main(String[] args)
{ new Root(); }
private CarManager carManager;
}
这里是循环调用的classCarManager,问题我已经标出来了
带 // !!
的区域
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);
}
}
这是汽车 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; }
}
问题是您从不调用 CarManager.setUp()
方法,这意味着链表始终为空,因此 return 值始终为 false。
我在一个名为 findCar() 的方法中编写了一个循环,它遍历链表汽车,并检查用户输入的 ID 是否与 Car 实例中的 Id 相同,即 Car 实例 Id从 Car class 中的 getId() 方法收集。但是它总是返回false,有谁知道这是为什么?您会看到对其他 class 的引用,例如 Clients 和 Person,但是为了简单起见,我省略了它们,但是如果拥有该代码有助于获得答案,我会添加它。非常感谢任何帮助
这是根Class
import java.io.*;
public class Root
{ public Root() {
new CarManager();
}
public static void main(String[] args)
{ new Root(); }
private CarManager carManager;
}
这里是循环调用的classCarManager,问题我已经标出来了 带 // !!
的区域 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);
}
}
这是汽车 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; }
}
问题是您从不调用 CarManager.setUp()
方法,这意味着链表始终为空,因此 return 值始终为 false。