Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有更有效的方法月换算_Java_Performance - Fatal编程技术网

Java 有没有更有效的方法月换算

Java 有没有更有效的方法月换算,java,performance,Java,Performance,我有一个小任务要完成,要求用户输入一个月或等值的数字,然后返回输入月份的数值,或与输入数值对应的月份。限制条件如下: -它不能包含任何类型的GUI -我必须使用BufferedReader进行输入 -我必须至少使用一个Switch语句 如果有人有任何想法,我们将不胜感激。 我的代码如下: /** * Month task * * @author Dan Foad * @version 0.01 */ import java.io.IOException; import java.i

我有一个小任务要完成,要求用户输入一个月或等值的数字,然后返回输入月份的数值,或与输入数值对应的月份。限制条件如下: -它不能包含任何类型的GUI -我必须使用BufferedReader进行输入 -我必须至少使用一个Switch语句

如果有人有任何想法,我们将不胜感激。 我的代码如下:

/**
 * Month task
 * 
 * @author Dan Foad
 * @version 0.01
 */

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Months {
    public static void main(String []args) throws IOException {
        int iInput;
        boolean isParseable;
        String szInput;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Please type either the number of the month, or the month itself to convert.");
        System.out.print("> ");
        szInput = br.readLine();

        try {
            Integer.valueOf(szInput);
            isParseable = true;
        }
        catch(Exception e) {
            isParseable = false;
        }

        if (isParseable) {
            iInput = Integer.valueOf(szInput);
            System.out.println(numberToMonth(iInput));
        }
        else {
            szInput = szInput.toLowerCase();
            System.out.println(monthToNumber(szInput));
        }
        return;
    }

    public static String numberToMonth(int iMonth) {
        String MonthReturn;
        switch(iMonth) {
            case (1): MonthReturn = "January"; break;
            case (2): MonthReturn = "February"; break;
            case (3): MonthReturn = "March"; break;
            case (4): MonthReturn = "April"; break;
            case (5): MonthReturn = "May"; break;
            case (6): MonthReturn = "June"; break;
            case (7): MonthReturn = "July"; break;
            case (8): MonthReturn = "August"; break;
            case (9): MonthReturn = "September"; break;
            case (10): MonthReturn = "October"; break;
            case (11): MonthReturn = "November"; break;
            case (12): MonthReturn = "December"; break;
            default: MonthReturn = "0"; break;
        }
        return MonthReturn;
    }

    public static int monthToNumber(String szMonth) {
        int MonthReturn;
        switch(szMonth) {
            case ("january"): MonthReturn = 1; break;
            case ("february"): MonthReturn = 2; break;
            case ("march"): MonthReturn = 3; break;
            case ("april"): MonthReturn = 4; break;
            case ("may"): MonthReturn = 5; break;
            case ("june"): MonthReturn = 6; break;
            case ("july"): MonthReturn = 7; break;
            case ("august"): MonthReturn = 8; break;
            case ("september"): MonthReturn = 9; break;
            case ("october"): MonthReturn = 10; break;
            case ("november"): MonthReturn = 11; break;
            case ("december"): MonthReturn = 12; break;
            default: MonthReturn = 0; break;
        }
        return MonthReturn;
    }
}
您也可以这样做:

private static final String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

public static String numberToMonth(int iMonth) {
  return (iMonth < 12 && iMonth > 0) ? monthNames[iMonth - 1] : "None";
}
private static final String[]monthNames={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
公共静态字符串numberToMonth(int iMonth){
返回(iMonth<12&&iMonth>0)?monthNames[iMonth-1]:“无”;
}
您可以将字符串“1”、“2”、“3”、…、“12”添加到monthToNumber方法中,这样您就不必使用String.valueOf将用户输入转换为整数。

这怎么样

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Calendar;
import static java.util.Calendar.APRIL;
import static java.util.Calendar.AUGUST;
import static java.util.Calendar.DECEMBER;
import static java.util.Calendar.FEBRUARY;
import static java.util.Calendar.JANUARY;
import static java.util.Calendar.JULY;
import static java.util.Calendar.JUNE;
import static java.util.Calendar.MARCH;
import static java.util.Calendar.MAY;
import static java.util.Calendar.NOVEMBER;
import static java.util.Calendar.OCTOBER;
import static java.util.Calendar.SEPTEMBER;

public class Months {
public static void main(String []args) throws IOException {
    Integer iInput = null;
    String szInput = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Please type either the number of the month, or the month itself to convert.");
    System.out.print("> ");
    szInput = br.readLine();

    boolean wasInt = false;
    try {
      iInput = Integer.valueOf(szInput);
      System.out.println(numberToMonth(iInput));
      wasInt = true;
    }
    catch(Exception e) {
    }

    if (! wasInt) {
        szInput = szInput.toLowerCase();
        System.out.println(monthToNumber(szInput));
    }
    return;
}

public static String numberToMonth(int iMonth) {
    switch(iMonth-1) {
        case (JANUARY): return "January"; 
        case (FEBRUARY): return "February"; 
        case (MARCH): return "March"; 
        case (APRIL): return "April"; 
        case (MAY): return "May"; 
        case (JUNE): return "June"; 
        case (JULY): return "July"; 
        case (AUGUST): return "August"; 
        case (SEPTEMBER): return "September"; 
        case (OCTOBER): return "October"; 
        case (NOVEMBER): return "November";
        case (DECEMBER): return "December";
    }
    return "Unknown";
}

public static int monthToNumber(String szMonth) {
  if (szMonth == null) {
    return 0;
  }
    switch(szMonth.toLowerCase()) {
        case ("january"): return 1 + JANUARY;
        case ("february"): return 1 + FEBRUARY;
        case ("march"): return 1 + MARCH;
        case ("april"): return 1 + APRIL;
        case ("may"): return 1 + MAY;
        case ("june"): return 1 + JUNE;
        case ("july"): return 1 + JULY;
        case ("august"): return 1 + AUGUST;
        case ("september"): return 1 + SEPTEMBER;
        case ("october"): return 1 + OCTOBER;
        case ("november"): return 1 + NOVEMBER;
        case ("december"): return 1 + DECEMBER;
    }
    return 0;
}
}

是的,在number-to-month方法中,您可以创建一个字符串数组,按时间顺序列出月份的名称,然后在给定输入值的索引减去1处获取字符串。如果您必须使用switch语句,那么这可能就是他们希望您转换月份的方式。您的代码工作正常吗?如果没有,您有什么具体的问题?我的代码工作得很好,我只是想知道是否有更有效的方法来执行此操作,因为目前有两个switch语句基本上执行相同的函数(尽管是相反的)似乎有点奇怪。不过我不知道如何改进。你也可以看看
enum
Heck,我只是把月份名称放在一个字符串数组中,然后使用switch()来确定转换的方向,只是为了让讲师感到困惑:)谢谢你的回复。你能解释一下为什么这个版本比我原来的版本更高效吗?它更短(代码效率),而且不使用临时变量(空间)。还有三件事-1)你可以优化空间或时间(或某些组合),2)你不应该在现实世界中过早优化,等到你发现之前和之后的瓶颈和基准点,让我看看我是否理解正确。您创建了一个包含月份的私有实例数组,将每个月的索引与其各自的编号相对应。您使用了
monthNames[iMonth-1]
,因为
i
通常会
1
更高。当您键入
something.numberToMonth(1)
时,它会以字符串格式返回月份吗?以及不大于12且小于0的强制条件。
的作用是什么?