Bad Operand types for binary operator && when trying to calculate birthdate from current date

huangapple go评论76阅读模式
英文:

Bad Operand types for binary operator && when trying to calculate birthdate from current date

问题

我正试图让用户输入年份、月份和日期,然后系统会基于所提供的输入计算出当前年龄,并在屏幕上显示出来。

以下是我代码的开头部分:

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("无效的 current_date");
		f = 1;
	}

我得到了一个“Bad Operand types for binary operator &&”的错误,我无法弄清楚为什么会出现这个错误,而且我对编程相当新手。感谢任何帮助。

英文:

I am trying to get a user to input YR MON DAY and then system calculate the current age based on the provided input and show the age on the screen.

Here is the beginning to my code:

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 &lt;= 01 &amp;&amp; current_date =&gt; 31) {
		System.out.println(&quot;Invalid current_date&quot;);
		f = 1;
	}

I am getting a "Bad Operand types for binary operator &&" I cannot figure out why, and I am fairly new to coding.
Thanks for any help

答案1

得分: 2

这是 &gt;= 而不是 =&gt;,所以 if(current_date&lt;=1 &amp;&amp; current_date&gt;=31)

(不要使用数字前缀 0,这会导致它们被解释为八进制)

英文:

It is &gt;= not =&gt; so if(current_date&lt;=1 &amp;&amp; current_date&gt;=31)

(don't use 0 as prefix for numbers, it causes them to be interpreted as octal)

答案2

得分: 1

这里你想要返回什么?距离某人生日还有多少天?你可以使用 Period 类的 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);
    
    
}
英文:

What are you trying to return here? The amount of days left before someones birthdate? You could use the between() method of Period class for that.

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);


}

答案3

得分: 1

如我在评论中已经提到的,问题是由于操作符的错误符号=&gt;。它应该是&gt;=。查看这个以了解更多关于操作符的内容。

除此之外,我可以看到你的逻辑存在一个严重的问题。你验证日期值的方式是一种天真的做法。我建议你使用下面所示的现成API来进行验证:

import java.time.DateTimeException;
import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        // 测试
        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("如果你看到这行打印,日期值是正确的。");
            // ...其余的代码
        } catch (DateTimeException e) {
            System.out.println(e.getMessage());
            f = 1;
        }
        // ...其余的代码
    }
}

输出:

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
如果你看到这行打印,日期值是正确的。
英文:

As I have already mentioned in the comment, the issue is because of the bad symbol, =&gt; for the operator. It should be &gt;=. Check this to learn more about operators.

Apart from that, I can see a serious problem with your logic. The way you are validating the date values is a naive way to do it. I suggest you do it using OOTB APIs as shown below:

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(&quot;If you see this line printed, the date values are correct.&quot;);
			// ...Rest of the code
		} catch (DateTimeException e) {
			System.out.println(e.getMessage());
			f = 1;
		}
		// ...Rest of code
	}
}

Output:

Invalid value for DayOfMonth (valid values 1 - 28/31): 45
Invalid date &#39;FEBRUARY 30&#39;
Invalid date &#39;APRIL 31&#39;
Invalid value for MonthOfYear (valid values 1 - 12): 15
If you see this line printed, the date values are correct.

答案4

得分: 0

以下是翻译好的内容:

不是更容易编写一个函数,用户提供年份、月份和日期,然后函数返回用户的年龄吗?以下是一个实现此功能的示例函数。

```java
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();
}

<details>
<summary>英文:</summary>

isn&#39;t it easier to write a function where the user will provide year, month and the day and it will return how old is the user? Below is an example of a function that does it.

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();
}


</details>



# 答案5
**得分**: -1

你之所以会得到错误是因为

你使用了 `if (current_date &lt;= 01 &amp;&amp; current_date =&gt;31) ` 而不是

    if (current_date &lt;= 01 &amp;&amp; current_date &gt;=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 &lt;= 01 &amp;&amp; current_date &gt;=31){
                System.out.println(&quot;无效的 current_date&quot;);
                f=1;
            }
    }

<details>
<summary>英文:</summary>

you are getting the error because 

 you are using `if (current_date &lt;= 01 &amp;&amp; current_date =&gt;31) ` instead of 

    if (current_date &lt;= 01 &amp;&amp; current_date &gt;=31)

after editing your code will be:


    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 &lt;= 01 &amp;&amp; current_date &gt;=31){
                System.out.println(&quot;Invalid current_date&quot;);
                f=1;
            }
    }


</details>



huangapple
  • 本文由 发表于 2020年4月6日 03:26:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61048242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定