Java 年龄计算误差
当我运行这段代码时,它说找不到主程序,因为我找不到主程序 publicstaticvoidmain(String[]参数),但它不允许我同时使用这两个参数。如何更正此问题Java 年龄计算误差,java,Java,当我运行这段代码时,它说找不到主程序,因为我找不到主程序 publicstaticvoidmain(String[]参数),但它不允许我同时使用这两个参数。如何更正此问题 import java.util.Calendar; import java.util.Scanner; public class Week6Methods4 { public int getAge(int year, int month, int day) { Date now = new Dat
import java.util.Calendar;
import java.util.Scanner;
public class Week6Methods4 {
public int getAge(int year, int month, int day) {
Date now = new Date();
int nowMonth = now.setMonth() + 1;
int nowYear = now.setYear() + 1900;
int result = nowYear - year;
if (month > nowMonth) {
result--;
} else if (month == nowMonth) {
int nowDay = now.getDate();
if (day > nowDay) {
result--;
}
}
return result;
}
public static void main(String[] args) {
System.out.println("Enter Date of Birth");
Scanner input = new Scanner(System.in);
int DOB = input.nextInt();
}
}
帮助您的提示不多 首先,您在日期类中使用的函数不推荐使用 1.getMonth() 2.getYear() Second,您必须在主方法之外定义方法
getAge
为什么?
public class Sample {
public void A() {
...
your code
...
}
public static void main(String[] args) {
...
your code
...
}
}
因为getAge
和main方法一样是单独的方法和唯一的方法
将此用作蓝图
public class Sample {
public void A() {
...
your code
...
}
public static void main(String[] args) {
...
your code
...
}
}
在您的新帖子中,您需要为数据类添加适当的导入 将此添加到导入中
import java.util.Date;
你必须使用getter而不是setter
int nowMonth = now.setMonth() + 1;
int nowYear = now.setYear() + 1900;
将它们更改为
now.getMonth()
和now.getYear()
显示您的所有代码,并请正确缩进。在您的程序中添加一个public static void main(String…args)
方法,有关一些想法,请参阅…您的程序需要一个public static void main()
。如果您尝试添加时出现错误,请显示该代码和该错误。我尝试实现您所说的内容,但仍然不起作用,我认为我没有完全理解这一点。现在它在语句Date Now=new Date();找不到symbol@guinea2你有没有用困扰你的新代码更新你的帖子?是的,我有了新的代码,我真的很感谢你的帮助。我正在努力学习javawhere是public类的一部分?导入后你看到它缺少什么了吗?