从其他文件中调用函数"无法找到符号"

时间:2017-08-14 11:03:40

标签: java function class methods

尝试从各个类中调用一些函数进行练习。

我们被告知要使用以下函数调用,

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

当我尝试MonthName.month_name();它说它是一个非静态方法,它实际上是一个静态方法,如下所示,我通过调用它来排序,

MonthName monthObject = new MonthName();

将问题排除在外。但是我尝试调用的其他所有函数都能获得它无法找到符号的消息。

我试图在

中调用函数的类
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;
}

}

2 个答案:

答案 0 :(得分:1)

MonthOffset不是monthoffset。你有语法错误,尝试使用Eclipse或IntelliJ从IDE获得一些帮助。

答案 1 :(得分:1)

这是语法错误。您没有类MonthOffset等。将您的类的名称从monthoffset更改为Monthoffset。

公约是here