输入和输出日期级Java [英] Input and Output date class Java

查看:165
本文介绍了输入和输出日期级Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的一类日期项目中,用户输入的日期和输出日期为3个不同的格式。
一)MM / DD / YYYY
B)MONTHNAME DD,YYYY
C)DDD,YYYY今年(日期)。

I'm doing a class date project where user input the date and output the date into 3 difference formats. a) MM/DD/YYYY b) MonthName DD, YYYY c) DDD, YYYY (date of the year).

我被困在一个点上,这里输出结果的一部分。在这里,我得到了什么至今

I got stuck at one point, where output the result for part a. Here what I got so far

import java.util.Scanner;
public class Implementation
{
    private static int month;
    private static int day;
    private static int year;
    private static final int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    public static void Date(String args[])
    {
        Scanner input = new Scanner(System.in);
        while(year != -1)
        {    
        System.out.print("Enter month: ");
        month = input.nextInt();
        System.out.print("Enter day: ");
        day = input.nextInt();
        System.out.print("Enter year: ");
        year = input.nextInt();

        System.out.printf("\nMM/DD/YYYY: %d/%d/%d");
        System.out.printf("\nMonth DD/YYYY: ");
        System.out.println("\nDDD YYYY: \n");
        }    
    }    
    public Implementation(int month, int day, int year)
        {
        if (month <= 0 || month > 12)
            throw new IllegalArgumentException(
            "month (" + month + ") must be 1-12");
        if (day <= 0 | (day > daysPerMonth[month] && !(month == 2 && day == 29)))
            throw new IllegalArgumentException
            ("day (" + day + ") out-of-range for the specified month and year");
        if (month == 2 && day == 29 && !(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
            throw new IllegalArgumentException
            ("day (" + day + ") out-of-range for the specified month and year");

        this.month = month;
        this.day = day;
        this.year = year;
        }
    public String toString()
    {
        return String.format("%d/%d/%d", month, day, year);
    }
}

我应该把这个 System.out.printf之后(\\ NMM / DD / YYYY数:%d /%D /%D); 显示出结果(具有有效的月,日和年)。我没有做过其他两个选项呢。我是初学者,在这个项目非常沮丧。请人帮忙吗?

What should I put right after this System.out.printf("\nMM/DD/YYYY: %d/%d/%d"); to show up the result (with valid month,day,and year). I haven't done the others two options yet. I'm beginner and very frustrated in this project. Anyone please help?

推荐答案

当你用printf像下面这行要输出一些文字,你需要告诉的方法,你真正想要打印的东西。

When you want to output some text using printf like the following line, you need to tell the method what you actually want it to print.

有关这一行:

System.out.printf("\nMM/DD/YYYY: %d/%d/%d");

您需要把月,日和年入的printf方法的变量,否则该方法将只打印什么是双引号中。

You need to put in the variables for the month, day and year into the printf method otherwise the method will just print what's inside the double quotes.

所以正确的方式做到这一点这里是这样的:

So the correct way to do it here is this:

System.out.printf("\nMM/DD/YYYY: %d/%d/%d", month, day, year);

Java将在参数的逗号后替换变量中的值%d个符号,打印出的文字。第一%d由第一变量(在这种情况下,月)和第二%d个替换被第二可变(日)代替等。 (%d来当变量是一个int,%F是当它是一个浮动等)

Java will replace the %d symbols with the values inside the variables after the comma in the arguments and print out the text. The first %d is replaced by the first variable (in this case "month") and the second %d is replaced by the second variable ("day") and so on. (%d is used when the variable is an int, %f is used when it's a float, etc)

Java的字符串格式化文档

这篇关于输入和输出日期级Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆