Java 从其他文件调用函数”;Can';“找不到符号”;
尝试调用不同类中的函数进行练习 我们被告知使用以下函数调用Java 从其他文件调用函数”;Can';“找不到符号”;,java,function,class,methods,Java,Function,Class,Methods,尝试调用不同类中的函数进行练习 我们被告知使用以下函数调用 MonthName.month_name(); WeekdayName.weekday_name(); MonthOffset.month_offset(); WeekdayCalculator.is_leap(); 当我尝试MonthName.month_name()时;它说它是一个非静态的方法,实际上它是一个静态的方法,如下图所示,我通过调用 MonthName monthObject = new MonthName(); 解决
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;
}
}蒙特霍夫塞特不是蒙特霍夫塞特。如果出现语法错误,请尝试使用Eclipse或IntelliJ从IDE获得一些帮助。这是语法错误。您没有MonthOffset等类。请将您的类的名称从MonthOffset更改为MonthOffset
惯例是。在您发布的代码中,我只看到一个方法名
month\u name
。它位于类monthoffset
中,需要一个int参数。很明显,MonthName.month\u name()
将不起作用。另外请注意Java是区分大小写的monthoffset
和monthoffset
不是一回事。好吧……在粘贴代码时,我粘贴了monthoffset而不是monthname作为类,不像我的ide上那样。我删除了所有内容,然后重新开始,仍然是完全相同的问题。类名现在完全按原样压缩了;在类为monthname时工作?但是我可以使用monthname monthObject=new monthname()从monthname类调用month_name()函数;也许您还有其他名称正确的类。IMO在提到的代码中,这是不可能的。