简单的一类程序java介绍麻烦

时间:2017-06-29 23:27:09

标签: java

我已经做了几个小时的工作了,虽然这是今晚到期的,但我今天得到了智齿,麻醉让我很容易混淆。

我需要的是类中的两种方法," toString",它采用dd / mm / yyyy并打印出来,以及#34; advance"它修改了一天+ 1。 当我检查修改日期时,我会收到:

Initial date: 88/8/8888

Modified date: 88/0/8888




    int day, month, year, newDay;
    String decision, dummy ;        
    Scanner read = new Scanner(System.in);

    public static void main(String[] args) {
        Date dateInstance = new Date();
        dateInstance.toString();
        dateInstance.advance();
    }

    public String toString() {
        System.out.println("Enter day (mm/xx/yyyy): ");
        day = read.nextInt();
        System.out.println("Enter month (xx/dd/yyyy): ");
        month = read.nextInt();
        System.out.println("Enter year (mm/dd/xxxx): ");
        year = read.nextInt();

        System.out.println("Initial date: "+month+"/"+day+"/"+year);
        System.out.println("Modified date: "+month+"/"+newDay+"/"+year);

        return null;

        /*
        String decision = read.nextLine();
        System.out.println("Would you like to display the date, and the modified date? (Y / N): ");

        if(decision == "N") {
            System.out.println("'N' Selected");
        }else if(decision == "Y") {
            System.out.println("Initial date: "+month+"/"+day+"/"+year);
            System.out.println("Modified date: "+month+"/"+newDay+"/"+year);
        }
        return dummy;
        */
    }

    public int advance() {
        newDay = day + 1;
        return newDay;
    }

1 个答案:

答案 0 :(得分:0)

以下是一些可以帮助您入门的代码。我猜你的老师不希望你重写Date的toString()方法,但实际上用它来创建你自己的函数,以另一种格式显示输出(提示 - 相反)。

    Date dateInstance = new Date(System.currentTimeMillis());
    Date dateForward = new Date(dateInstance.getTime() + 1000*60*60*24); //put this in a method
    System.out.println(dateInstance.toString()); //use the split method to extract and rearrange the date
    System.out.println(dateForward.toString());

查看Date Object

的Java 8文档

输出

2017-06-29
2017-06-30