0

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

我对 java 很陌生,并且遇到了两个我迄今为止无法克服的编译器错误(从 36 下降,哈哈)。编译器错误是“不是语句”。两者都引用带有 else 语句的行。代码如下(显然仍然是在制品,还不是完整的程序) -

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 */

    }
}
4

3 回答 3

4

删除 if 语句后的分号

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

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

于 2013-08-28T04:34:18.767 回答
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

现在,它应该编译了。

于 2013-08-28T04:48:04.243 回答
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>0true或的结果false。现在下一个条件不再有意义。

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

   if (byear >0){
        if (byear <13); 
        {
            byear=(byear + 2000);
        }
        else  (byear >13 && byear <100); 
        {
            byear=(byear + 1900);   
        } 
    }  // end if statement
于 2013-08-28T04:39:09.623 回答