Java:内部测试类编译错误-打印开关块的所有情况



编辑:这是一个语法问题-在发布这篇文章的时候,我是一个完全的新手,并且没有使用合适的IDE,所以我向社区道歉。此外,请尽量避免假设这个网站上的每个人都是男人。

我现在正在和我的内部类DateTest搏斗。这个错误信息在编译后出现,我还没能弄清楚如何修复这个错误。为了在Date类中打印出switch块的所有12种情况,我已经遍历了许多行代码来编写测试类。

下面包含了错误和我的代码。另外值得一提的是,我是在一个在线IDE中编写这篇文章的,并且在CodeChef中进行了双重检查。

这是我的代码:

public class Date {  
    public static int daysInMonth(int month) {  
    /** method uses a switch block and takes in a month number (1 for January, 2 for February, etc.) and returns the number of days in a month. **/  
        switch (month) {  
            case 1:  
            case 3:  
            case 5:  
            case 7:  
            case 8:  
            case 10:  
            case 12:  
                return 31;   
                break;  
            case 2:  
                return 28;  
                break;  
            case 4:  
            case 6:  
            case 9:  
            case 11:  
                return 30;  
                break;  
            default:   
                System.out.println("Invalid month.");  
                break;  
        }  
        return month;  
    }  
    public class DateTest {  
        /*test class that prints out 12 lines like: "January is 31 days long.", "February...", etc.*/   
        int month; /* <-- compilation error had been here due to missing curly brace (error = "class expected") */  
    }  
    public static void main (String [] args) {  
        month = Date.daysInMonth(1);  
        System.out.println("January is " +month+  " days long.");  
        month = Date.daysInMonth(2);  
        System.out.println("February is " +month+ " days long.");  
        month = Date.daysInMonth(3);  
        System.out.println("March is " +month+ " days long.");  
        month = Date.daysInMonth(4);  
        System.out.println("April is " +month+ " days long.");  
        month = Date.daysInMonth(5);  
        System.out.println("May is " +month+ " days long.");  
        month = Date.daysInMonth(6);  
        System.out.println("June is " +month+ " days long.");  
        month = Date.daysInMonth(7);  
        System.out.println("July is " +month+ " days long.");  
        month = Date.daysInMonth(8);  
        System.out.println("August is " +month+ " days long.");  
        month = Date.daysInMonth(9);  
        System.out.println("September is " +month+ " days long.");  
        month = Date.daysInMonth(10);  
        System.out.println("October is " +month+ " days long.");  
        month = Date.daysInMonth(11);  
        System.out.println("November is " +month+ " days long.");  
        month = Date.daysInMonth(12);  
        System.out.println("December is " +month+ " days long.");   
    }  
}

(编辑:这是我在这里问的第一个问题,我刚刚开始学习Java & &;如何完全独立编程-我对任何初学者的建议:使用一台足够体面的计算机,可以支持基本的IDE语法高亮显示,而不是一个没有这个的在线环境,因为Java/c++标点符号可能会给你带来噩梦,当你不熟悉它

month应该只是main函数中的一个本地成员。只要在第一次使用它时添加一个类型声明,就可以了:

public class DateTest {
    public static void main (String [] args) {
        int month = Date.daysInMonth(1);
        // rest of code comes here
    }
}

内部类不允许有静态方法。只允许静态类和顶层类

就像这样做:

import java.util.Scanner;
public class Date
{
    public static boolean isLeapYear(int year)
    {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static int daysInMonth(int month, boolean isLeapYear)
    {
        /**
         * method uses a switch block and takes in a month number (1 for
         * January, 2 for February, etc.) and returns the number of days in a
         * month.
         **/
        switch (month)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            return 31;
        case 2:
            if (isLeapYear)
            {
                return 29;
            }
            else
            {
                return 28;
            }
        case 4:
        case 6:
        case 9:
        case 11:
            return 30;
        default:
            System.out.println("Invalid month.");
            break;
        }
        return month;
    }
    /*
     * test class that prints out 12 lines like: "January is 31 days long.",
     * "February...", etc.
     */
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int year = in.nextInt();
        boolean isLeapYear = isLeapYear(year);
        int month = 0;
        month = Date.daysInMonth(1, isLeapYear);
        System.out.println("January is " + month + " days long.");
        month = Date.daysInMonth(2, isLeapYear);
        System.out.println("February is " + month + " days long.");
        month = Date.daysInMonth(3, isLeapYear);
        System.out.println("March is " + month + " days long.");
        month = Date.daysInMonth(4, isLeapYear);
        System.out.println("April is " + month + " days long.");
        month = Date.daysInMonth(5, isLeapYear);
        System.out.println("May is " + month + " days long.");
        month = Date.daysInMonth(6, isLeapYear);
        System.out.println("June is " + month + " days long.");
        month = Date.daysInMonth(7, isLeapYear);
        System.out.println("July is " + month + " days long.");
        month = Date.daysInMonth(8, isLeapYear);
        System.out.println("August is " + month + " days long.");
        month = Date.daysInMonth(9, isLeapYear);
        System.out.println("September is " + month + " days long.");
        month = Date.daysInMonth(10, isLeapYear);
        System.out.println("October is " + month + " days long.");
        month = Date.daysInMonth(11, isLeapYear);
        System.out.println("November is " + month + " days long.");
        month = Date.daysInMonth(12, isLeapYear);
        System.out.println("December is " + month + " days long.");
    }
}

最新更新