我可以将args传递给变量吗?

时间:2018-02-17 00:44:27

标签: java parsing static arguments main

所以我创建了整个程序,认为我让用户输入数据,但事实证明我们需要使用args []来实现这一点,现在我不断破坏我的代码试图修复它。任何帮助将不胜感激。我还需要修复我的格式..

继承人的任务: 编写一个程序Calendar.java,它接受两个命令行参数m和y,并打印出y年第m个月的月历。你应该只使用条件和循环(没有数组,因为我们还没有覆盖它)。例如,Calendar 2 2009的输出应为:

---------------------
    February 2009
---------------------
 Su Mo Tu We Th Fr Sa
  1  2  3  4  5  6  7
  8  9 10 11 12 13 14
 15 16 17 18 19 20 21
 22 23 24 25 26 27 28
---------------------

这就是我所知道的:在闰年期间,2月28日会增加一天。一年是闰年,如果它可被400整除或可被4整除而不是100。例如:2000年和2004年是闰年,但1900年不是。

星期几:给定一个月的m,一天的d和一年的y,一周的相应日期计算如下:

y0 = y - (14 - m) / 12
x = y0 + y0/4 - y0/100 + y0/400
m0 = m + 12 * ((14 - m) / 12) - 2
d0 = (d + x + (31*m0)/12) mod 7 // 0 for Sunday, ..., 6 for Saturday

这是我的代码:

import java.util.Scanner;

public class Calendar {

private static int numDays = 0;
private static int h = 0;
//this method is used to find the given year is leap year or not

public static boolean leap(int year) {
    if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) {
        return true;
    } else {
        return false;
    }
}
//this method is used to find first day of the year

public static void firstDayOfYear(int year) {
    int month = 13;
    year--;
    h = (1 + (int) (((month + 1) * 26) / 10.0) + year + (int) (year / 4.0) + 6 * (int) (year / 100.0) + (int) (year / 400.0)) % 7;
    String dayName = "";
    switch (h) {
        case 0:
            dayName = "Saturday";
            break;
        case 1:
            dayName = "Sunday";
            break;
        case 2:
            dayName = "Monday";
            break;
        case 3:
            dayName = "Tuesday";
            break;
        case 4:
            dayName = "Wednesday";
            break;
        case 5:
            dayName = "Thursday";
            break;
        default:
            dayName = "Friday";
            break;
    }

}

//this method is used to find first day of the Month
public static void firstDayOfMonth(int month, int year) {
    if (month == 1 || month == 2) {
        month += 12;
        year--;
    }
    h = (1 + (int) (((month + 1) * 26) / 10.0) + year + (int) (year / 4.0) + 6 * (int) (year / 100.0) + (int) (year / 400.0)) % 7;
    String dayName = "";
    switch (h) {
        case 0:
            dayName = "Saturday";
            break;
        case 1:
            dayName = "Sunday";
            break;
        case 2:
            dayName = "Monday";
            break;
        case 3:
            dayName = "Tuesday";
            break;
        case 4:
            dayName = "Wednesday";
            break;
        case 5:
            dayName = "Thursday";
            break;
        default:
            dayName = "Friday";
            break;
    }

}
//this method is used to find first number of days in a month

public static void DaysInMonth(int month, int year) {
    int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if (month == 2 && leap(year)) {
        days[month] = 29;
    }
    numDays = days[month];

}
//this method is used to print the calender

public static void calendar(int month, int year) {
    String[] monthNames = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    System.out.println(" " + monthNames[month] + " " + year);
    System.out.println("Su Mo Tu We Th Fr Sa");
    for (int i = 0; i < h - 1; i++) {
        System.out.print(" ");
    }
    for (int i = 1; i <= numDays; i++) {
        System.out.printf("%2d ", i);
        if (((i + h - 1) % 7 == 0) || (i == numDays)) {
            System.out.println();
        }
    }
}

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter month (1-12): ");
    int month = input.nextInt();
    if (month < 1 || month > 12) {
        System.out.println("Invalid month. Valids inputs are 1-12.");
        System.exit(0);
    }
    System.out.print("Enter year: ");
    int year = input.nextInt();
    if (year < 1753) {
        System.out.println("Invalid year. Valid inputs are 1753 and beyond.");
        System.exit(0);
    }

    firstDayOfYear(year);
    firstDayOfMonth(month, year);
    DaysInMonth(month, year);
    calendar(month, year);
}
}

这是我得到的错误: 运行时错误:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Calendar.main(Calendar.java:92)

图意味着我不允许使用扫描仪而且我应该使用args []

1 个答案:

答案 0 :(得分:0)

您说您需要使用命令行参数,但您正在使用Scanner获取输入。如果你的程序有一个自动化测试程序,它可能会传递参数,例如Calendar 2 2009,其中2和2009是命令行参数。那么为什么不从arg []而不是Scanner获取输入,因为系统无法检查你是否真的使用过数组。

PS:我没有足够的声誉来评论这就是为什么这是一个答案。遗憾。