如果陈述逻辑

时间:2013-04-04 10:50:17

标签: java if-statement logic

我的任务是为我正在做的课程创建一个小的java程序,我在下面的代码块中遇到了一些问题:

        System.out.println("Please enter your first name:");
    userName = sc.nextLine();
    System.out.println("What year were you born in?");
    birthYear = sc.nextInt();
    System.out.println("In the 'dd.mm' format, what are the day and month of your birth?");
    dayMonth = sc.nextDouble();
    //Cast the user input of type double to an int for the day of Birth
    dayOfBirth = dayMonth.intValue();

    //Cast the double value for month and cast it to an int
    dayMonth = dayMonth - dayOfBirth;
    dayMonth = dayMonth * 100;
    dayMonth = Math.rint(dayMonth);
    monthOfBirth = dayMonth.intValue();
if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941)
{
    if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11)
    {
        System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear);
    }
        else if(monthOfBirth != 9 || monthOfBirth != 10 || monthOfBirth != 11)
        {
            System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear);
        }
}
if(birthYear != 2013 || birthYear != 2001 || birthYear != 1989 || birthYear != 1977 || birthYear != 1965 || birthYear != 1953 || birthYear != 1941)
{
     if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11)
    {
        System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear);
    }   
        else if(monthOfBirth != 9 || monthOfBirth != 10 || monthOfBirth != 11)
        {
            System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear);
        }
}

但是我的输出结果如下:

Please enter your first name:

Samuel

What year were you born in?

1977

In the 'dd.mm' format, what are the day and month of your birth?

09.09

Samuel was born during Spring on September 9 in Snake year 1977

Samuel was born during Spring on September 9 

我显然不想重复输出,但是我很难看到if语句中的逻辑导致它重复出现。

非常感谢任何帮助。

全心全意,

~Sam。

8 个答案:

答案 0 :(得分:1)

不要单独使用2 if,而是将它们设为if-elseif-else if

if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941){
    ....
}
else if(birthYear != 2013 || birthYear != 2001 ||...){
....
}

因为,如果您提供if-if,则会执行if个语句,而如果您提供if-else if,则仅执行第二个else if如果第一个if语句失败。

答案 1 :(得分:1)

您可以选择以下解决方案之一:

1)使用&&运算符而不是||在第二个if语句

2)if(){} else {} // Kamal's Solution

答案 2 :(得分:1)

最好使用集合而不是多个if(... == .. || ... == .. || ...)

像这样:

List<String> wrongYears = Arrays.asList("2013", "2001", "1989", "1977", ...);

并检查:

if(wrongYears.contains(birthYear)
{
 ...
}

和月份数相同

你的逻辑错误很明显

答案 3 :(得分:0)

尽可能使用else语句,所以如果你测试2013年,2001年...... 紧接着,发表一个其他声明

答案 4 :(得分:0)

你应该替换第二个,如果一切都是!=用一个简单的'else',因为你已经检查过任何一个陈述是否为真。因此,简单地写else就逻辑上相同并且更具可读性。

if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941){
    ....
}
else { //not snake year
    ....
}

答案 5 :(得分:0)

问题在于

if(birthYear != 2013 || birthYear != 2001 || birthYear != 1989 || birthYear != 1977 || birthYear != 1965 || birthYear != 1953 || birthYear != 1941

如果你仔细观察,我们确保表达总是导致尝试。一次只有一个可以是假的,其他所有都是真的,因此最终总是如此(困惑?)。说出生年份是2013年,这使得第一个条件(birthyear!= 2013)错误,但所有其他都是真的。采取任何其他的例子,你会看到我想说的。

实际上,一个小修改将为您修复代码。您已经检查过if (birthyear is in given years),一个没有任何条件的简单条款对您有效。

if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941)
{
    if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11)
    {
        System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear);
    }
        else// NO CHECK REQUIRED NOW
        {
            System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear);
        }
}
//SEE THIS
else{
     if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11)
    {
        System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear);
    }   
//AND
        else 
        {
            System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear);
        }
}

答案 6 :(得分:0)

第二个if部分是错误的:

if(birthYear != 2013 || birthYear != 2001 || ....)

因为它总是如此。 只需查看一个视图示例: 如果你有2012年,它不是2013年,所以你的表达成真。 如果你拿2013年那么第一部分就变成了假,但是当你评估2013!= 2001时,它再次成为现实。

你可能想要的是

if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941)
{
.....
}
else {
.....
}

顺便说一句:如果你想否定表达式(x == A || x == B || x == C)那么逻辑否定是(x!= A&amp;&amp; x!= B &amp;&amp; x!= C)。

答案 7 :(得分:0)

答案在你的条件中。

假设dob = 1

if(dob == 1 || dob == 2 || dob == 3 ...)

将评估为true,因为dob == 1

if (dob != 1 || dob != 2 || dob != 3 ...)

也会评估为true,因为dob!= 2

您要使用的是AND / &&而不是OR / ||