我是一年级学生,对java编程非常陌生,我真的希望你们能解决我的问题。我有以下代码,它在Eclipse上运行完美,但无法在Linux中运行终端。代码如下:
Person.java
package src2;
public class Person implements Runnable {
protected Bathroom bathroom;
private boolean isMale;
// Minimum time to idle
private int minIdleTime = 1000;
// Maximum time to idle
private int maxIdleTime = 1300;
// Minimum time to spend in a stall
private int minInStallTime = 1500;
// Maximum time to spend in a stall
private int maxInStallTime = 2000;
private long startWaitTime;
public Person(Bathroom bathroom, boolean isMale) {
this.bathroom = bathroom;
this.isMale = isMale;
}
/**
* Determines how long a person has been waiting in the queue.
*
* @return The length a person has been waiting.
*/
public long getWaitingTime() {
return System.currentTimeMillis() - startWaitTime;
}
/**
* Returns gender of person.
*
* @return true indicates male, false indicates female.
*/
public boolean isMale() {
return isMale;
}
/**
* The run method - called when thread starts.
*/
public void run() {
// Constantly loop
// Wait random amount of time before queuing to use the bathroom (±Æ¶?)
try {
Thread.sleep((int) (minIdleTime + (maxIdleTime - minIdleTime)
* Math.random()));
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Queue up for the Bathroom.
try {
// Remember the time person started to queue.
startWaitTime = System.currentTimeMillis();
// Queue in the bathroom - it will sleep until woken up.
bathroom.enqueuePerson(this);
}
catch (InterruptedException e1) {
e1.printStackTrace();
}
// Now 'bathroom' has woken person up, meaning they are in a stall. (Sb¬~Yþ)
try {
// Do something for a random amount of time to represent
// 'using' the bathroom
Thread.sleep((int) (minInStallTime + (maxInStallTime - minInStallTime)
* Math.random()));
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Finished using the bathroom, so inform it.
try {
bathroom.offerStall(this);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// Loop to begin cycle again.
while (true) {
// Wait random amount of time before queuing to use the bathroom
try {
Thread.sleep((int) (minIdleTime + (maxIdleTime - minIdleTime)
* Math.random()));
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Queue up for the Bathroom.
try {
// Remember the time person started to queue.
startWaitTime = System.currentTimeMillis();
// Queue in the bathroom - it will sleep until woken up.
bathroom.enqueuePerson(this);
}
catch (InterruptedException e1) {
e1.printStackTrace();
}
// Now 'bathroom' has woken person up, meaning they are in a
// stall.
try {
// Do something for a random amount of time to represent
// 'using' the bathroom
Thread.sleep((int) (minInStallTime + (maxInStallTime - minInStallTime)
* Math.random()));
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Finished using the bathroom, so inform it.
try {
bathroom.offerStall(this);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Loop to begin cycle again.
}
}
}
Bathroom.java
package src2;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
public class Bathroom {
private LinkedList bathroomQueue = new LinkedList();
private LinkedList stallOccupants = new LinkedList();
private int noOfStalls = 5;
// Used as measurements
private int totalFemales = 0;
private long femaleQueueTimeTotal = 0;
private int totalMales = 0;
private long maleQueueTimeTotal = 0;
private int MinUseBathroomTime = 1500;
private int MaxUseBathroomTime = 2000;
private int exitMales = 0;
private int exitFemales = 0;
Person in = null; //the person do not know the sex yet.
/*
* The JFrame to update in order to show measurements. Not really a very OO
* way of doings things (e.g., forcing unrelated classes to rely on each
* other), but works fine for demonstration purposes.
*/
private RunBathroom windowThread;
public Bathroom(RunBathroom windowThread) {
this.windowThread = windowThread;
}
/**
* Enters a {@link Person} Person into the queue for the bathroom
*
* @param person The {@link Person} Person to enter into the queue
* @throws InterruptedException
*/
public void enqueuePerson(Person person) throws InterruptedException {
Person nextInStall;
// Add person to the queue and then check to see if they're next in line.
// Lock the object to prevent a context switch allowing someone else
// enter the queue first
synchronized (this) {
bathroomQueue.add(person);
nextInStall = offerStall(null);
}
// If not next in line, put person to sleep
if (person != nextInStall) {
synchronized (person) {
person.wait();
}
}
}
/**
* Method the check who is next to enter bathroom
*
* @param remove Person to remove from a stall (can be null)
* @return The Person
* @throws InterruptedException
*/
public synchronized Person offerStall(Person remove) throws InterruptedException {
Person occupiersGender = (Person) stallOccupants.peek(); //the person in the bathroom.
Person nextPerson = (Person) bathroomQueue.peek(); //the person in the bathroom queue.
boolean maleInControl = false; // = false (is a male);
// If given, remove the person from a stall
if (remove != null) {
removePerson(remove, stallOccupants);
}
Person in = null; //the person do not know the sex yet.
// If no-one is queuing, or if the stalls are full, return null.
if (bathroomQueue.size() == 0 || stallOccupants.size() >= noOfStalls) {
in = null;
} else {
// Otherwise find next the next person to enter bathroom
/* Person occupiersGender = (Person) stallOccupants.peek(); //the person in the bathroom.
Person nextPerson = (Person) bathroomQueue.peek(); //the person in the bathroom queue.
boolean maleInControl; // = false (is a male); */
// If someone is in the bathroom, find out which sex
if (occupiersGender != null) {
maleInControl = occupiersGender.isMale(); // stallOc
}
// If nobody is in bathroom, then give control to whoever is next in queue
else {
maleInControl = nextPerson.isMale();
}
// If someone exists in the queue
if (nextPerson != null) {
// If the next person is male, and males are in control of the bathroom, or the bathroom is empty
if (nextPerson.isMale()
&& (maleInControl || stallOccupants.size() == 0)) {
in = nextPerson;
}
// Otherwise, if next person is female, and females are in control, or the bathroom is empty
else if (!nextPerson.isMale()
&& (!maleInControl || stallOccupants.size() == 0)) {
maleInControl = false; //this is a female.
in = nextPerson;
}
// Otherwise, the next person in the queue cannot yet enter.
else {
in = null;
}
}
}
// If a person was chosen to enter bathroom
if (in != null) {
Random r=new Random();
int i = r.nextInt(5);
// Remove them from the queue, and update measurements/window
if (in.isMale()) {
while(in.isMale()){
for(int j = 0; j <= i; j++){
removePerson(in, bathroomQueue);
maleQueueTimeTotal += in.getWaitingTime();
totalMales++;
Thread.sleep((int) (MinUseBathroomTime + (MaxUseBathroomTime - MinUseBathroomTime)
*Math.random()));
windowThread.update(in.isMale(),totalMales, exitMales);
}
for(int j = 0; j<=i; j++){
Thread.sleep((int) (MinUseBathroomTime + (MaxUseBathroomTime - MinUseBathroomTime)
*Math.random()));
exitMales++;
windowThread.update(in.isMale(), totalMales, exitMales);
}
in = (Person) bathroomQueue.getFirst();
}
} else {
while(!in.isMale()){
for(int j = 0; j <= i; j++){
removePerson(in, bathroomQueue);
femaleQueueTimeTotal += in.getWaitingTime();
totalFemales++;
Thread.sleep((int) (MinUseBathroomTime + (MaxUseBathroomTime - MinUseBathroomTime)
*Math.random()));
windowThread.update(in.isMale(), totalFemales, exitFemales);
}
for(int j = 0; j<=i; j++){
Thread.sleep((int) (MinUseBathroomTime + (MaxUseBathroomTime - MinUseBathroomTime)
*Math.random()));
exitFemales++;
windowThread.update(in.isMale(),totalFemales, exitFemales);
}
in = (Person) bathroomQueue.getFirst();
}
}
// Add the person to a stall
stallOccupants.add(in);
// Notify the person so as to wake them up
synchronized (in) {
in.notify();
}
}
// Return the person
return in;
}
/**
* Removes a person from a list
*
* @param person Person to remove
* @param list Queue/list to remove from
*/
private void removePerson(Person person, LinkedList list) {
// Iterate through the list and remove the person if found
Iterator i = list.iterator();
while (i.hasNext()) {
if (i.next() == person) {
i.remove();
}
}
}
}
RunBathroom.java
package src2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
/**
* Application demonstrating a fair solution to the "Unisex Bathroom" problem.
* Creates the GUI and upon clicking "Start", initialises the {@link Bathroom} and populates
* the queue with females and males.
* <p/>
* A random arrival pattern is used.
public class RunBathroom extends JFrame implements Runnable, ActionListener {
private JLabel totalMales, totalFemales, totalUsers, malestate, femalestate, exitmale, exitfemale, emptymale, emptyfemale;
private JButton startItem = new JButton("Start");
private JButton quit = new JButton("Quit");
private Bathroom bathroom;
// Boolean representing the gender currently occupying the bathroom; true indicates male, false indicates female.
private boolean isMale = true;
public static void main(String[] args) {
new Thread(new RunBathroom()).start();
}
/**
* Constructor which sets up all the necessary components of the GUI.
*/
public RunBathroom() {
super("Unisex Toilet Problem");
setSize(700, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel bottomPanel = new JPanel();
// Add start button
startItem.setText("Start");
startItem.addActionListener(this);
bottomPanel.add(startItem);
startItem.setEnabled(false);
// Add quit button
quit.addActionListener(this);
bottomPanel.add(quit);
JPanel mainPanel = new JPanel(new GridLayout(2, 1));
JPanel topPanel = new JPanel(new GridLayout(5, 2));
JPanel topPanel2 = new JPanel (new GridLayout(3, 2));
totalMales = new JLabel("0");
totalMales.setHorizontalAlignment(SwingConstants.CENTER);
totalFemales = new JLabel("0");
totalFemales.setHorizontalAlignment(SwingConstants.CENTER);
totalUsers = new JLabel("0");
totalUsers.setHorizontalAlignment(SwingConstants.CENTER);
malestate = new JLabel ("0");
malestate.setHorizontalAlignment(SwingConstants.CENTER);
femalestate = new JLabel ("0");
femalestate.setHorizontalAlignment(SwingConstants.CENTER);
exitmale = new JLabel ("0");
exitmale.setHorizontalAlignment (SwingConstants.CENTER);
exitfemale = new JLabel ("0");
exitfemale.setHorizontalAlignment(SwingConstants.CENTER);
emptymale = new JLabel ("0");
emptymale.setHorizontalAlignment(SwingConstants.CENTER);
emptyfemale = new JLabel ("0");
emptyfemale.setHorizontalAlignment(SwingConstants.CENTER);
// Add labels
topPanel.add(new JLabel("Total males: "));
topPanel.add(totalMales);
topPanel.add(new JLabel("Total females: "));
topPanel.add(totalFemales);
topPanel.add(new JLabel ("Male state: "));
topPanel.add(malestate);
topPanel.add(new JLabel ("Female state: "));
topPanel.add(femalestate);
topPanel.add(new JLabel("Number of exit male: "));
topPanel.add(exitmale);
topPanel.add(new JLabel("Number of exit female: "));
topPanel.add(exitfemale);
topPanel.add(new JLabel(" "));
topPanel.add(emptymale);
topPanel.add(new JLabel(" "));
topPanel.add(emptyfemale);
topPanel.add(new JLabel("Total Number of persons: "));
topPanel.add(totalUsers);
mainPanel.add(topPanel);
mainPanel.add(topPanel2);
Container contentPane = getContentPane();
contentPane.add(mainPanel, "Center");
contentPane.add(bottomPanel, "South");
startItem.setEnabled(true);
}
/**
* When the thread starts, display the GUI.
*/
public void run() {
setVisible(true);
}
/**
* Exits on close of window
*
* @param e Windowevent (close)
*/
public void windowClosed(WindowEvent e) {
System.exit(0);
}
/**
* Listens for mouse-presses on menu items.
*
* @param evt The ActionEvent performed
*/
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
// If Quit is clicked, exit program.
if (source == quit) {
System.exit(0);
}
// If Start is clicked, create a bathroom and add male and female
// persons to it.
else if (source == startItem) {
startItem.setEnabled(false);
bathroom = new Bathroom(this);
for (int i = 0; i < 10; i++) {
new Thread(new Person(bathroom, !isMale)).start();
new Thread(new Person(bathroom, isMale)).start();
}
}
}
/**
* Updates the labels on the GUI
*
* @param isMale Sex referring to the label to update
* @param averageTime The average time that gender has had to wait
* @param totalOfSex The total number of that gender which has used to bathroom
*/
public void update(boolean isMale, int totalOfSex, int exitOfpeople) {
if (isMale) {
totalMales.setText(Integer.toString(totalOfSex));
exitmale.setText(Integer.toString(exitOfpeople));
malestate.setText("GOING");
femalestate.setText("WAITING");
if (totalOfSex == exitOfpeople){
emptymale.setText("Everyone can go!");
emptyfemale.setText(" ");
}else{
emptymale.setText("Only for Male");
emptyfemale.setText(" ");
}
} else {
totalFemales.setText(Integer.toString(totalOfSex));
exitfemale.setText(Integer.toString(exitOfpeople));
femalestate.setText("GOING");
malestate.setText("WAITING");
if (totalOfSex == exitOfpeople){
emptyfemale.setText("Everyone can go!");
emptymale.setText(" ");
}else{
emptyfemale.setText("Only for Female");
emptymale.setText(" ");
}
}
totalFemales.getText();
totalUsers.setText(Integer.toString(Integer.parseInt((totalFemales.getText()))
+ Integer.parseInt((totalMales.getText()))));
}
}
仍有17个错误表示无法找到符号。我尽力了解但仍然无法理解。不能有人帮忙吗? :( 显示的错误如下:
Bathroom.java:30: error: cannot find symbol
Person in = null; //the person do not know the sex yet.
^
symbol: class Person
location: class Bathroom
Bathroom.java:37: error: cannot find symbol
private RunBathroom windowThread;
^
symbol: class RunBathroom
location: class Bathroom
Bathroom.java:39: error: cannot find symbol
public Bathroom(RunBathroom windowThread) {
^
symbol: class RunBathroom
location: class Bathroom
Bathroom.java:49: error: cannot find symbol
public void enqueuePerson(Person person) throws InterruptedException {
^
symbol: class Person
location: class Bathroom
Bathroom.java:73: error: cannot find symbol
public synchronized Person offerStall(Person remove) throws InterruptedException {
^
symbol: class Person
location: class Bathroom
Bathroom.java:73: error: cannot find symbol
public synchronized Person offerStall(Person remove) throws InterruptedException {
^
symbol: class Person
location: class Bathroom
Bathroom.java:180: error: cannot find symbol
private void removePerson(Person person, LinkedList list) {
^
symbol: class Person
location: class Bathroom
Bathroom.java:50: error: cannot find symbol
Person nextInStall;
^
symbol: class Person
location: class Bathroom
Bathroom.java:75: error: cannot find symbol
Person occupiersGender = (Person) stallOccupants.peek(); //the person in the bathroom.
^
symbol: class Person
location: class Bathroom
Bathroom.java:75: error: cannot find symbol
Person occupiersGender = (Person) stallOccupants.peek(); //the person in the bathroom.
^
symbol: class Person
location: class Bathroom
Bathroom.java:76: error: cannot find symbol
Person nextPerson = (Person) bathroomQueue.peek(); //the person in the bathroom queue.
^
symbol: class Person
location: class Bathroom
Bathroom.java:76: error: cannot find symbol
Person nextPerson = (Person) bathroomQueue.peek(); //the person in the bathroom queue.
^
symbol: class Person
location: class Bathroom
Bathroom.java:83: error: cannot find symbol
Person in = null; //the person do not know the sex yet.
^
symbol: class Person
location: class Bathroom
Bathroom.java:142: error: cannot find symbol
in = (Person) bathroomQueue.getFirst();
^
symbol: class Person
location: class Bathroom
Bathroom.java:161: error: cannot find symbol
in = (Person) bathroomQueue.getFirst();
^
symbol: class Person
location: class Bathroom
15 errors
答案 0 :(得分:1)
import Person.java;
import RunBathroom.java;
使用.java后缀查看包名称是如此不常见。请检查Person类的import语句和包名。
答案 1 :(得分:0)
如果您可以从ide运行软件而不是从命令行运行软件,则需要检查命令行和ide配置是否存在任何差异。特别是: