當我嘗試運行(使用)最後一個方法時,它開始運行並永不停止。另外,當我嘗試打印出作業時,元素沒有正確打印。它帶有奇怪的字母。我在發佈整個類,因爲我猜測我除了runJod和runAll方法以外,還在其他地方犯了一個錯誤。有誰能告訴我我需要做什麼來解決這個問題嗎?如何打印列表中的元素和所有元素
import java.util.ArrayList;
/** * /
公共類JobQueue
{ 私人ArrayListmyJobInQueue; //做
private ArrayList<Job>myFinishedJobs;// a list of compleated job
private int myJobDuration; //duration if one job
private int myTimeLeft;//total time left
/**
* Constructor for objects of class JobQueue
*/
public JobQueue()
{
myJobInQueue = new ArrayList<Job>();
myFinishedJobs = new ArrayList<Job>();
myJobDuration =0;
myTimeLeft=0;
}
/**
* Return the list of jobs that have not been completed (including the current job).
*/
public ArrayList<Job> getPendingJobs()
{
return myJobInQueue;
}
/**
* Return the list of jobs that have been completed.
*/
public ArrayList<Job> getCometedJobs()
{
return myFinishedJobs;
}
/**
* Return the job at the front of the pending queue, or null if the queue is empty.
*/
public Job getCurrentJob()
{
if(myJobInQueue!=null)
{
Job FirstJobInTheQueue = myJobInQueue.get(0);
return FirstJobInTheQueue;
}
else
{
return null;
}
}
/**
* Return the amount of time left on the clock (as an integer)
*/
public int getTimeLeft()//Ok
{
return myTimeLeft;
}
/**
* Return the total duration of all the pending jobs(as an integer).
*/
public int getTotalDuration()
{
int myTimeLeft= 0;
for(int i = 0; i<myJobInQueue.size();i++)
{
int num = myJobInQueue.getDuration(i); //I think this line is wrong.
myTimeLeft = myTimeLeft + num ;
}
return myTimeLeft;
}
/**
* Add a Job to the end of the Queue
*/
public void addJob(Job job)
{
if(job!=null)
{
myJobInQueue.add(job);
}
}
/**
* Add the specified number of seconds to the clock.
*/
public void addTime(int seconds)
{
if(seconds>0)
{
myTimeLeft = myTimeLeft + seconds;
}
}
/**
* Run the first job on the queue if there is enough time on the clock and the job queue list is not empty.
* And move the job to the finished jobs list.
*/
public void runAJob(){
if(!myJobInQueue.isEmpty())
{
myJobDuration = myJobInQueue.get(0).getDuration();
if (myJobDuration < myTimeLeft)
{
myTimeLeft = myTimeLeft - myJobDuration;
myFinishedJobs.add(myJobInQueue.get(0));
System.out.println("A job is running: " + myJobInQueue.get(0).getName());
myJobInQueue.remove(0);
}
else
{
System.out.println("Not enogth running time left, please add time on the clock.");
}
}
else
{
System.out.println("No pending job on the list.");
}
}
/**
* Run all the jobs on the queue in order until it runs out of time.
*/
public void runAll()
{
for(int i = 0; myTimeLeft > 0 && myTimeLeft > myJobDuration;i++);
{
runJob();
}
System.out.println("Job can not be run, not enough time left.");
}
}
是行'的System.out.println( 「Ajob運行」 + myJobInQueue.get(0));'不打印可言,或者將它打印意想不到的事情? –
例如,我正在打印出作業正在運行作業[email protected]。 – user1721548