If语句的编译器错误

时间:2013-08-28 04:32:08

标签: java

我正在尝试实现将DD / MM / YY输入转换为DD / MM / YYYY输入的程序的一部分。我使用if语句要求程序检查输入数字是否为<13,如果是,则将2000添加到输入数字 - 如果输入12,则给出2012的结果。如果该数字> 13且<100,我已要求该程序添加1900。

我是java的新手,遇到了两个编译器错误,到目前为止我无法克服这些错误(从36开始,哈哈)。编译器错误是“不是声明”。两者都引用带有else语句的行。代码如下(显然仍然是WIP而不是已完成的程序) -

import java.util.*;

public class FindDay4Birthdate
{
    public static void main(String[] args)
    {
        String dayInput = "";
        String monthInput = "";
        String yearInput = "";
        int bday;
        int bmonth;
        int byear;

        String daysList[] = {
            "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
        };
        String monthList[] = {
            "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
        };

        Scanner sc = new Scanner(System. in );
        sc.useDelimiter("[-/.\\s]");

        System.out.print("Please enter your date of birth (dd/mm/yyyy) - ");

        if (sc.hasNext())
        {
            dayInput = sc.next();
            monthInput = sc.next();
            yearInput = sc.next();

            bday = Integer.parseInt(dayInput);
            bmonth = Integer.parseInt(monthInput);
            byear = Integer.parseInt(yearInput);


        } // end if statement

        if (byear = (byear > 0));
        {
            if (byear = (byear < 13))
            {
                byear = (byear + 2000);
            }
            else(byear = (byear > 13 && byear < 100));
            {
                byear = (byear + 1900);
            }
        } // end if statement


        bmonth -= 1; //set month to correct array

        String day = daysList[bday];
        String month = monthList[bmonth];

        int yearCount = (byear - 1901);
        int daysInYear = 365;
        int dayCount = (daysInYear * yearCount);

        System.out.println("You were born on " + day + " " + bday + " " + month + " " + byear);

        System.out.println(yearCount + " " + daysInYear + " " + dayCount); // test output only

        /* Todo
         *
         *TASK 2 -
         *Implement daysPerMonth - Feb set to 28 days
         *Use count to determine correct day for birthdate
         *
         *TASK 3 -
         *User input needs to allow char based months and 2 digit years need to be converted to 4 digit years
         *Implement leap years */

    }
}

3 个答案:

答案 0 :(得分:4)

在if语句后删除半冒号

if (byear=(byear >0)); 同样也适用于其他地方

if之后的半冒号会将其视为块的结尾。因此,您定义的else不被视为if语句之前的

的一部分

答案 1 :(得分:1)

修复编译器错误后,您将遇到此块的逻辑错误

    if (byear=(byear >0));{
        if (byear=(byear <13)); 
        {
            byear=(byear + 2000);
        }
        else  (byear =(byear >13 && byear <100)); 
        {
            byear=(byear + 1900);   
        } 
    }  // end if statement

byear=(byear >0)表示byear现在是byear>0的结果,truefalse。现在接下来的条件已经不再合理了。

byear = <condition>中的所有此类if语句更改为<condition>。即(不要复制粘贴,了解差异)

   if (byear >0){
        if (byear <13); 
        {
            byear=(byear + 2000);
        }
        else  (byear >13 && byear <100); 
        {
            byear=(byear + 1900);   
        } 
    }  // end if statement

答案 2 :(得分:1)

这部分是你的问题。

if (byear = (byear > 0));
{
    if (byear = (byear < 13))
    {
        byear = (byear + 2000);
    }
    else(byear = (byear > 13 && byear < 100));
    {
        byear = (byear + 1900);
    }
} // end if statement

您犯的第一个错误是if语句末尾的分号。

if (byear = (byear > 0)); // This ; symbol is to be removed

这导致在不执行任何语句的情况下结束if块。

下一个错误是您尝试比较值的方式。这里。

byear = (byear > 0);

如果byear大于0,则此语句将byear变量分配给true,否则使其成为false,这会导致编译器错误,因为byear是一个整数而不是布尔值。所以将你的第一个if语句改为

if (byear > 0)

请参阅,无需将其分配给自己。以同样的方式删除比较中的所有分配会使您的错误成为

if (byear > 0);
{
    if (byear < 13)
    {
        byear = (byear + 2000);
    }
    else(byear > 13 && byear < 100);
    {
        byear = (byear + 1900);
    }
} // end if statement

现在,它应该编译。