2017-08-14 65 views
-1

嘗試從各個類的某些函數調用練習。從其他文件調用函數「找不到符號」

我們被告知使用下面的函數調用,

MonthName.month_name(); 
WeekdayName.weekday_name(); 
MonthOffset.month_offset(); 
WeekdayCalculator.is_leap(); 

當我試圖MonthName.month_name();它說,它是一個非靜態方法,它是如以下INFACT靜態方法,我整理通過調用它像,

MonthName monthObject = new MonthName(); 

哪個整理這個問題了。但是我嘗試調用的其他函數都是msg,它找不到符號。

類我試圖調用函數

public class callingfunctionsfromotherfiles 



public static void main(String[] args) 
{ 
    // TODO code application logic here 

    Scanner scan = new Scanner(System.in); 
    String finalDate=""; 
    System.out.println("Pleaase enter your date of birth in this format dd mm yyyy"); 
    String dob=scan.nextLine(); // date of birth entered by the user 


    String d = dob.substring(0,2); //split the dob into 3 parts 
    String m = dob.substring(3, 5); 
    String y = dob.substring(6, 10); 

    int day = Integer.parseInt(d); //convert dob strings to int 
    int month = Integer.parseInt(m); 
    int year = Integer.parseInt(y); 
    MonthName monthObject = new MonthName(); 
// MonthName.month_name(month); non static??? 

System.out.println(finalDate= "You where born on " + week_day(day, month, year) +", " + monthObject.month_name(month) + ", "+ day + ", " + year); 

} 



public static String week_day(int day, int month, int year) // returns a string value for the week day with integer parameters of day,month,year 
    { 

     MonthOffset offsetObject = new MonthOffset(); //"Can't find symbol" 
     WeekDayCalculator leapObject = new WeekDayCalculator(); //"Can't find symbol" 
     WeekDayName wkdayObject = new WeekDayName(); //"Can't find symbol" 

    int yy = year - 1900; 
    int total = yy/4 + yy + day + offsetObject.month_offset(month); 
    // weekDayCalculator.month_offset(); 

    //WeekDayCalculator.is_leap(); 
    boolean leap = leapObject.is_leap(year); //pass "year" into the is_leap method which returns true or false 
    if(leap == true && month == 1 || month == 2) //if it is a leap year and the month == 1(january) 2(february) subtract 1 from the total 
    { 
     total-= -1; 
    } 


    total = total %7; //total is remainder of (total/7) 
    String finalDay = wkdayObject.weekday_name(total); // call weekday_name and pass in total, giving finalDay a string value from weekday_name 

    return finalDay; 




} 

這是類&功能,當我創建一個對象,工作,但也告訴我函數是非靜態的時候,確實是。

public class monthname 
{ 
public static String month_name(int num) 

{ 
    String month =""; 

    if(num == 1) 
    { 
     month ="January"; 
    } 

    else if(num == 2) 
    { 
     month ="Feburary"; 
    } 

    else if(num == 3) 
    { 
     month ="March"; 
    } 

    else if(num == 4) 
    { 
     month ="April"; 
    } 

    else if(num == 5) 
    { 
     month ="May"; 
    } 

    else if(num == 6) 
    { 
     month ="June"; 
    } 

    else if(num == 7) 
    { 
     month ="July"; 
    } 

    else if(num == 8) 
    { 
     month ="August"; 
    } 

    else if(num == 9) 
    { 
     month ="September"; 
    } 

    else if(num == 10) 
    { 
     month ="October"; 
    } 

    else if(num == 11) 
    { 
     month ="November"; 
    } 

    else if(num == 12) 
    { 
     month ="December"; 
    } 

    else 
    { 
     month ="error"; 
    } 
    return month; 

} 
} 

而這是其中一個類&功能找不到符號。如果需要,會發布更多內容

public class monthoffset 
{ 
public static int month_offset(int month) 
{ 
    int offset =0; 

    if (month == 1) 
    { 
     offset=1; 
    } 
    else if (month == 2) 
    { 
     offset = 4; 
    } 
    else if (month == 3) 
    { 
     offset = 4; 
    } 
    else if (month == 4) 
    { 
     offset = 0; 
    } 
    else if (month == 5) 
    { 
     offset = 2; 
    } 
    else if (month == 6) 
    { 
     offset = 5; 
    } 
    else if (month == 7) 
    { 
     offset = 0; 
    } 
    else if (month == 8) 
    { 
     offset = 3; 
    } 
    else if (month == 9) 
    { 
     offset = 6; 
    } 
    else if (month == 10) 
    { 
     offset = 1; 
    } 
    else if (month == 11) 
    { 
     offset = 4; 
    } 
    else if(month ==12) 
    { 
     offset=6; 
    } 

    else 
    { 
     offset = -1; 
    } 


    return offset; 
} 

}

+0

我只在您發佈的代碼中看到一個方法名稱'month_name'。它在類「monthoffset」中,需要一個int參數。所以顯然'MonthName.month_name()'不起作用。 –

+0

另請注意,Java區分大小寫。 'monthoffset'和'MonthOffset'不是一回事。 –

+0

好的......當粘貼我的代碼時,我粘貼了monthoffset而不是monthname作爲類,而不是像我的ide。我刪除everythig並重新開始,仍然是完全相同的問題。現在類名已經完全按原樣複製了。 –

回答

1

MonthOffset不monthoffset。你有語法錯誤,嘗試使用Eclipse或IntelliJ從IDE獲得一些幫助。

+0

好的,但然後爲什麼MonthName();班級是月份時工作嗎? –

1

這是語法錯誤。你沒有上課MonthOffset等。將你的班級名稱從monthoffset更改爲Monthoffset。

約定是here

+0

但我可以使用MonthName monthObject = new MonthName()從monthname類中調用month_name()函數; –

+0

也許你有其他正確名稱的類。 IMO在提到的代碼中是不可能的。 – Nequeq