gpt4 book ai didi

java - java中如何解决这个 'cannot find symbol'

转载 作者:行者123 更新时间:2023-12-01 10:01:44 27 4
gpt4 key购买 nike

我是一名一年级学生,对 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.
}
}
}

浴室.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:错误:找不到符号
人= null;//这个人还不知道性别。
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:37:错误:找不到符号
私有(private)运行浴室窗口线程;
^
符号:类 RunBathroom
地点:类(class)浴室
Bathroom.java:39:错误:找不到符号
公共(public)浴室(RunBathroom windowThread){
^
符号:类 RunBathroom
地点:类(class)浴室
Bathroom.java:49:错误:找不到符号
公共(public)无效 enqueuePerson(Person person) 抛出 InterruptedException {
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:73:错误:找不到符号
公共(public)同步的人offerStall(人删除)抛出InterruptedException {
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:73:错误:找不到符号
公共(public)同步的人offerStall(人删除)抛出InterruptedException {
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:180: 错误:找不到符号
私有(private)无效removePerson(人,LinkedList列表){
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:50:错误:找不到符号
下一个进入失速的人;
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:75:错误:找不到符号
人员占用者性别=(人员)stallOccupants.peek();//浴室里的人。
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:75:错误:找不到符号
人员占用者性别=(人员)stallOccupants.peek();//浴室里的人。
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:76:错误:找不到符号
人 nextPerson = (Person) BathroomQueue.peek();//洗手间排队的人。
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:76:错误:找不到符号
人 nextPerson = (Person) BathroomQueue.peek();//洗手间排队的人。
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:83:错误:找不到符号
人= null;//这个人还不知道性别。
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:142:错误:找不到符号
in = (Person) BathroomQueue.getFirst();
^
符号:类 Person
地点:类(class)浴室
Bathroom.java:161:错误:找不到符号
in = (Person) BathroomQueue.getFirst();
^
符号:类 Person
地点:类(class)浴室
15 个错误

最佳答案

import Person.java;
import RunBathroom.java;

看到带有 .java 后缀的包名真是太不寻常了。请检查您的导入语句和 Person 类的包名称。

关于java - java中如何解决这个 'cannot find symbol',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36766897/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com