2012-10-07 46 views
-1

我收到以下錯誤消息從我的代碼,Java作業類構造函數中的錯誤消息。請幫助

找不到符號 - 變量作業名

我們必須創建兩個簡單場作業名稱和作業長一類叫做工作。

我不明白爲什麼我會收到此錯誤消息。代碼如下。提前感謝您的幫助。

import java.util.ArrayList; 
public class Job 
    { 
// instance variables - replace the example below with your own 
private String name; 
private int duration; 
private boolean isComplete; 

/** 
* Constructor for objects of class Job 
*/ 
public Job(String name, int duration) 
{ 
    // initialise instance variables 
    jobName = name; 
    jobDuration = duration; 


} 
/** 
* Accessor method for job name. 
* 
* @param y a sample parameter for a method 
* @return  value of job. 
*/ 
public String getName() { 
    { 
     // put your code here 
     return jobName; 
    } 
} 

/** 
* Accessor method for job duration. 
* 
* @param y a sample parameter for a method 
* @return value of job duration. 
* 
*/ 

public int getDuration() { 
    { 
     return jobDuration; 
    } 

} 

/** 
* Run method which prints. 
* 
* @param y a sample parameter for a method. 
* @return 
*/ 

public void run(String name, int duration) { 
    if (isComplete) 
    { 
     System.out.print("JOB COMPLETE" + jobName);    
    } 

} 
} 
+0

請接受其中一個答案作爲您的問題的答案和upvote如果它幫助你。當有人投入時間幫助你時,這是最不可能的。你已經提出了六個問題並收到了答案,但從未接受過其中一個。 – MarchingHome

回答

1

代替jobNamejobDuration,你需要說this.namethis.duration,因爲這就是那些字段命名。

+0

非常感謝您的幫助。現在編譯沒有錯誤。對不起,我是這樣的小白菜! – user1605782

+0

現在輪到你接受答案了。如果你沒有開始接受答案,人們將停止給你解決方案。 :) – Matt

0

您將您的私人數據成員命名爲「名稱」,但您一直將其稱爲「jobName」。

0

您的構造函數正在使用名爲jobNamejobDuration的新變量,該變量未定義。如下更正:

/** 
    * Constructor for objects of class Job 
    */ 
    public Job(String name, int duration){ 
     // initialise instance variables 
     name = name; 
     duration = duration; 
    } 

而且它是更好的做法是使用的參數和成員變量不同的名稱或前成員變量,例如使用this

/** 
    * Constructor for objects of class Job 
    */ 
    public Job(String aName, int aDuration) { 
     // initialise instance variables 
     name = aName; 
     duration = aDuration; 
    } 

/** 
    * Constructor for objects of class Job 
    */ 
    public Job(String name, int duration) { 
     // initialise instance variables 
     this.name = name; 
     this.duration = duration; 
    } 
0

改變這種

private String name; 
private int duration; 

這樣:

private String jobName; 
private int jobDuration; 
0

它,因爲你沒有jobName變量在類我覺得你有命名爲name

0

你給了,

private String name; 
private int duration; 

和您所使用的沒有定義jobName, jobDuration。請聲明這些或使用,

this.name = name; 
this.duration = duration;