尝试从当前日期计算出生日期时二元运算符 && 的错误操作数类型
Bad Operand types for binary operator && when trying to calculate birthdate from current date
我试图让用户输入 YR MON DAY,然后系统根据提供的输入计算当前年龄并在屏幕上显示年龄。
这是我的代码的开头:
static void checkAgeFormat(int current_date, int current_month,
int current_year, int birth_date,
int birth_month, int birth_year) {
int f = 0;
if(current_date <= 01 && current_date => 31) {
System.out.println("Invalid current_date");
f = 1;
}
我得到一个 "Bad Operand types for binary operator &&" 我不知道为什么,而且我对编码还很陌生。
感谢您的帮助
是>=
不是=>
所以if(current_date<=1 && current_date>=31)
(不要使用 0 作为数字的前缀,这会导致它们被解释为八进制)
编写一个用户将提供年、月和日的函数,并且 return 用户多大了?这不是更容易吗?下面是执行此操作的函数示例。
public static int getYears(int year, int month, int day)
{
LocalDate yearOfBirth = LocalDate.of(year, month, day);
LocalDate currentDate = LocalDate.now();
Period period = Period.between(yearOfBirth, currentDate);
return period.getYears();
}
你收到错误是因为
您正在使用 if (current_date <= 01 && current_date =>31)
而不是
if (current_date <= 01 && current_date >=31)
编辑代码后将是:
static void checkAgeFormat(int current_date, int current_month, int current_year, int birth_date, int birth_month, int birth_year) {
int f=0
if (current_date <= 01 && current_date >=31){
System.out.println("Invalid current_date");
f=1;
}
}
你想return做什么?离某人的生日还有多少天?为此,您可以使用 Period
class 的 between()
方法。
static void checkAgeFormat(int current_date, int current_month,
int current_year, int birth_date,
int birth_month, int birth_year) {
LocalDate birthDate = LocalDate.of(birth_year, birth_month, birth_date);
long daysLeft = Period.between(LocalDate.now(), birthDate).get(ChronoUnit.DAYS);
}
正如我在评论中已经提到的,问题是因为操作符的符号 =>
错误。应该是>=
。检查 this 以了解有关运算符的更多信息。
除此之外,我可以看出您的逻辑存在严重问题。您验证日期值的方式是一种天真的方式。我建议您使用 OOTB API 来执行此操作,如下所示:
import java.time.DateTimeException;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
// Tests
checkAgeFormat(45, 10, 2017, 10, 10, 2010);
checkAgeFormat(30, 2, 2017, 10, 10, 2010);
checkAgeFormat(31, 4, 2017, 10, 10, 2010);
checkAgeFormat(30, 15, 2017, 10, 10, 2010);
checkAgeFormat(30, 4, 2020, 10, 10, 2010);
}
static void checkAgeFormat(int current_day, int current_month, int current_year, int birth_day, int birth_month,
int birth_year) {
int f = 0;
LocalDate currentDate, birthDate;
try {
currentDate = LocalDate.of(current_year, current_month, current_day);
birthDate = LocalDate.of(birth_year, birth_month, birth_day);
System.out.println("If you see this line printed, the date values are correct.");
// ...Rest of the code
} catch (DateTimeException e) {
System.out.println(e.getMessage());
f = 1;
}
// ...Rest of code
}
}
输出:
Invalid value for DayOfMonth (valid values 1 - 28/31): 45
Invalid date 'FEBRUARY 30'
Invalid date 'APRIL 31'
Invalid value for MonthOfYear (valid values 1 - 12): 15
If you see this line printed, the date values are correct.
我试图让用户输入 YR MON DAY,然后系统根据提供的输入计算当前年龄并在屏幕上显示年龄。
这是我的代码的开头:
static void checkAgeFormat(int current_date, int current_month,
int current_year, int birth_date,
int birth_month, int birth_year) {
int f = 0;
if(current_date <= 01 && current_date => 31) {
System.out.println("Invalid current_date");
f = 1;
}
我得到一个 "Bad Operand types for binary operator &&" 我不知道为什么,而且我对编码还很陌生。 感谢您的帮助
是>=
不是=>
所以if(current_date<=1 && current_date>=31)
(不要使用 0 作为数字的前缀,这会导致它们被解释为八进制)
编写一个用户将提供年、月和日的函数,并且 return 用户多大了?这不是更容易吗?下面是执行此操作的函数示例。
public static int getYears(int year, int month, int day)
{
LocalDate yearOfBirth = LocalDate.of(year, month, day);
LocalDate currentDate = LocalDate.now();
Period period = Period.between(yearOfBirth, currentDate);
return period.getYears();
}
你收到错误是因为
您正在使用 if (current_date <= 01 && current_date =>31)
而不是
if (current_date <= 01 && current_date >=31)
编辑代码后将是:
static void checkAgeFormat(int current_date, int current_month, int current_year, int birth_date, int birth_month, int birth_year) {
int f=0
if (current_date <= 01 && current_date >=31){
System.out.println("Invalid current_date");
f=1;
}
}
你想return做什么?离某人的生日还有多少天?为此,您可以使用 Period
class 的 between()
方法。
static void checkAgeFormat(int current_date, int current_month,
int current_year, int birth_date,
int birth_month, int birth_year) {
LocalDate birthDate = LocalDate.of(birth_year, birth_month, birth_date);
long daysLeft = Period.between(LocalDate.now(), birthDate).get(ChronoUnit.DAYS);
}
正如我在评论中已经提到的,问题是因为操作符的符号 =>
错误。应该是>=
。检查 this 以了解有关运算符的更多信息。
除此之外,我可以看出您的逻辑存在严重问题。您验证日期值的方式是一种天真的方式。我建议您使用 OOTB API 来执行此操作,如下所示:
import java.time.DateTimeException;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
// Tests
checkAgeFormat(45, 10, 2017, 10, 10, 2010);
checkAgeFormat(30, 2, 2017, 10, 10, 2010);
checkAgeFormat(31, 4, 2017, 10, 10, 2010);
checkAgeFormat(30, 15, 2017, 10, 10, 2010);
checkAgeFormat(30, 4, 2020, 10, 10, 2010);
}
static void checkAgeFormat(int current_day, int current_month, int current_year, int birth_day, int birth_month,
int birth_year) {
int f = 0;
LocalDate currentDate, birthDate;
try {
currentDate = LocalDate.of(current_year, current_month, current_day);
birthDate = LocalDate.of(birth_year, birth_month, birth_day);
System.out.println("If you see this line printed, the date values are correct.");
// ...Rest of the code
} catch (DateTimeException e) {
System.out.println(e.getMessage());
f = 1;
}
// ...Rest of code
}
}
输出:
Invalid value for DayOfMonth (valid values 1 - 28/31): 45
Invalid date 'FEBRUARY 30'
Invalid date 'APRIL 31'
Invalid value for MonthOfYear (valid values 1 - 12): 15
If you see this line printed, the date values are correct.