需要帮助声明方法

时间:2011-11-30 20:10:35

标签: java

Java程序首先要求用户提供日期列表,然后打印最早的日期,最新日期和日期的平均年份,我无法编译以挽救我的生命,遇到麻烦在其他方法中声明方法,我对编程很新。

import java.util.*;

public class Proj6 {
   public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter a list of dates: ");
    String list = s.nextLine();

    StringTokenizer st = new StringTokenizer(list, ", ");
    String[] dates = new String[st.countTokens()];

    for (int i = 0; i < dates.length; i++) {
        dates[i] = st.nextToken();
    }


    Proj6.printEarliest(dates);
    Proj6.printLatest(dates);
    Proj6.printAvgYear(dates);
}

/**
 * getMonth returns the month in a date
 * of the form month/day/year
 *
 * @param date - A date of the form month/day/year
 * @return The month of the given date
 */
public static int getMonth(String date) {
    StringTokenizer st = new StringTokenizer(date, "/");
    return Integer.parseInt(st.nextToken());
}

/**


 * getDay returns the day in a date
 * of the form month/day/year
 *
 * @param date - A date of the form month/day/year
 * @return The day of the given date
 */
public static int getDay(String date) {
    StringTokenizer st = new StringTokenizer(date, "/");
    st.nextToken();
    return Integer.parseInt(st.nextToken());
}


/**
 * getYear returns the year in a date
 * of the form month/day/year
 *
 * @param date - A date of the form month/day/year
 * @return The year of the given date
 */
public static int getYear(String date) {
    StringTokenizer st = new StringTokenizer(date, "/");
    st.nextToken();
    st.nextToken();
    return Integer.parseInt(st.nextToken());
}

/**
 * printEarliest prints the date that comes chronologically
 * first from the dates array
 *
 * @param dates - An array of dates, all of the form month/day/year
 */
public static void printEarliest(String[] dates) {

    int i = 0;
    Proj6.getYear(dates);
    Proj6.getMonth(dates);
    Proj6.getDay(dates);
    for (i= 0; i < dates.length; i++) {
        if(getYear.dates [i]<=getYear.dates[i])

        st.nextToken();
    }
        System.out.println("Earliest date:" + dates[i]);
    }







/**
 * printLatest prints the date that comes chronologically
 * last from the dates array
 *
 * @param dates - An array of dates, all of the form month/day/year
 */
public static void printLatest(String[] dates) {


}

/**
 * printAvgYear prints the average year among all the dates in
 * the dates array
 *
 * @param dates - An array of dates, all of the form month/day/year
 */
public static void printAvgYear(String[] dates) {
    proj6.getYear();
   int sum = 0;
   int avg = 0;
    for (int i = 0; i < dates.length; i++) {
        sum = getYear + sum;
        dates[i] = st.nextToken();
}
avg = sum/dates.length;
System.out.println("Average:" + avg);

} }

1 个答案:

答案 0 :(得分:1)

在Java中,你没有在其他方法中声明方法(就像在javascript中一样)。

您有一个包含所有方法的类。他们可以互相打电话。

当你说“问”时,你的意思是你需要在终端中运行的程序提出问题,然后等待输入,然后在用户输入内容时采取行动吗?

如果是这种情况,例如,如果您想要询问用户的用户名,您可以这样做:

Console console = System.console();
String username = console.readLine("User Name? ");

这会挂出等待[enter]密钥并将键盘缓冲区中的任何内容收集到您的用户名字符串中......然后您可以继续对其进行操作。

欢迎来到Stack。当您的问题得到解答时,请不要忘记将答案标记为正确,并标记您认为最有帮助的答案。