如果传入无效值则抛出异常

Throw an exception if an invalid value is passed in

我有一个 class,现在我正在更改 setter 以在传入无效值时抛出异常。 它需要:

我的 class 是:

(我已经把setter改成抛出异常了,但是不行,我想我应该改一下main函数的构造函数,但是我不知道该怎么做。)

public class TodoItem {

    private String task;
    private int dueMonth;
    private int dueDay;
    private boolean isDone;

    // class variables
    private static int numItems;
    private static int numDone;

    // constructor
    public TodoItem(String taks,int day,int month) {
        this.task = task;
        dueDay = day;
        dueMonth = month;
        isDone = false;

        numItems++;
    }

    // second constructor
    public TodoItem(String task) {
        this.task = task;
        isDone = false;

        numItems++;
    }

    public static void WriteToFile(String a){
        a="toString.txt";
        String task;
        int dueMonth;
        int dueDay;
        boolean isDone;
    }
    // toString method
    public String toString() {
        return a+task + ", due: " + dueMonth + "/" + dueDay + (isDone?", done":", todo");
    }

    // getters
    public int getDueDay() {
        return dueDay;
    }

    public int getDueMonth() {
        return dueMonth;
    }

    // setters
    public void setDueDay(int day) throws Exception  {
        if (day >= 1 && day <=31) {
            dueDay = day;
        }
        else {
            throw new Exception ("Error:  invalid due day");
        }
    }

    public void setDueMonth(int month) throws Exception{
        if (month >= 1 && month <= 12) {
            dueMonth = month;
        }
        else {
            throw new Exception("Error:  invalid due month");
        }
    }

    // Checks off an item as being done.
    // If the item was already marked as done, don't increase the counter
    // (this was not specified in the problem set instructions).
    public void markAsDone() {
        if (!isDone) {
            isDone = true;
            numDone++;
        }
    }


    // returns the percentage of to-do list items completed
    public static double percentDone() {
        return (double) numDone/numItems*100;
    }

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // constructor 1
        TodoItemDone item1 = new TodoItemDone("Walk dog",12,3);
        TodoItemDone item2 = new TodoItemDone("Do 326 project",16,3);
        TodoItemDone item3 = new TodoItemDone("Put away winter boots",21,3);

        // constructor 2
        TodoItemDone item4 = new TodoItemDone("Buy groceries");
        TodoItemDone item5 = new TodoItemDone("Clean bathroom");
        TodoItemDone item6 = new TodoItemDone("Study for myself");


        // toString (and verify constructors)
        System.out.println("The 6 items are:");
        System.out.println(item1);
        System.out.println(item2);
        System.out.println(item3);
        System.out.println(item4);
        System.out.println(item5);
        System.out.println(item6);

        System.out.println();
        System.out.println("Setting due dates and months on the last 3:");
        // setDueDay
        item4.setDueDay(1);
        item5.setDueDay(5);
        item6.setDueDay(52);
        // setDueMonth
        item4.setDueMonth(12);
        item5.setDueMonth(6);
        item6.setDueMonth(0);

        System.out.println("The last 3 items are now:");
        System.out.println(item4);
        System.out.println(item5);
        System.out.println(item6);

        // Test percentDone() and markAsDone()
        System.out.println();
        System.out.println("About to complete some items:");
        System.out.println("percent done: " + percentDone());
        item1.markAsDone();
        System.out.println("Item 1 is now: " + item1);
        System.out.println("percent done: " + percentDone());

        item1.markAsDone();
        System.out.println("Item 1 is now: " + item1);
        System.out.println("percent done: " + percentDone());

        item2.markAsDone();
        System.out.println("Item 2 is now: " + item2);
        System.out.println("percent done: " + percentDone());
    }

更改此部分:

public TodoItem(String taks,int day,int month) {
    this.task = task;
    dueDay = day;
    dueMonth = month;
    isDone = false;

    numItems++;
}

对此:

public TodoItem(String task,int day,int month) {
    this.task = task;
    dueDay = day;
    dueMonth = month;
    isDone = false;

    numItems++;
}

您没有设置 this.task 来更正输入参数 taks。 我正在更改您的参数名称。 也许这不是主要问题,但请尝试改变它。

however, it doesn't work,

您定义 TodoItem class,但在 main() 中创建 TodoItemDone。当我将 TodoItem 更改为 TodoItemDone 时,我得到了结果:

The 6 items are:
null, due: 3/12, todo
null, due: 3/16, todo
null, due: 3/21, todo
Buy groceries, due: 0/0, todo
Clean bathroom, due: 0/0, todo
Study for myself, due: 0/0, todo

Setting due dates and months on the last 3:
Exception in thread "main" java.lang.Exception: Error:  invalid due day
    at com.github.vedenin.TodoItemDone.setDueDay(TodoItemDone.java:61)
    at com.github.vedenin.TodoItemDone.main(TodoItemDone.java:120)

正确抛出异常