简体   繁体   English

Java 带return语句的开关

[英]Java Switch with return statement

I need to return the value of a number input by the user from the command line.我需要返回用户从命令行输入的数字的值。

eg:例如:

java Test 5 

1 = January 1 = 一月

2 = February etc... 2 = 二月等...

I have tried using a switch statement, however, I receive an output:我尝试过使用 switch 语句,但是,我收到了 output:

May
5

when the input from command line.当从命令行输入时。 I'm not sure why this is.我不确定这是为什么。

The output should be: output 应该是:

/May

Here is what I have tried so far:这是我到目前为止所尝试的:

class theDate {

private int month;


public theDate(int theMonth){

    month = theMonth;

}

public theDate(){

}

public int getMonth() {

    switch (month) {

      case 1: System.out.println("January"); break;

      case 2: System.out.println("February"); break;

      case 3: System.out.println("March"); break;

      case 4: System.out.println("April"); break;

      case 5: System.out.println("May"); break;

      case 6: System.out.println("June"); break;

      case 7: System.out.println("July"); break;

      case 8: System.out.println("August"); break;

      case 9: System.out.println("September"); break;

      case 10: System.out.println("October"); break;

      case 11: System.out.println("November"); break;

      case 12: System.out.println("December"); break;



        }

        return month;

}



public String getDate(String[] datePartsObj) {


        month = Integer.parseInt(datePartsObj[0]);


        return "/" + month;

    }

}


public class Test {

private MyDate myDateObj;


public Test(){

}


public static void main(String [] args) {

    String[] datePartsObj;
    datePartsObj = args[0].split("/");
    int month = Integer.parseInt(datePartsObj[0]);

    theDate myDateObj = new theDate(month);

    System.out.println(myDateObj.getMonth());

    }

}

I'm very new to Java so any help would be greatly appreciated!我对 Java 非常陌生,因此将不胜感激任何帮助!

Seems like you want to return the name of the month by checking a given number.似乎您想通过检查给定的数字来返回月份的名称。

You have to change the method to return a String and not an int , then you can directly return the name in the matching case , like this:您必须更改方法以返回String而不是int ,然后您可以直接返回匹配case中的名称,如下所示:

public String getMonth() {
    switch (month) {
    case 1:
        // print the name of the month
        System.out.println("January");
        // and return it... same for ever other month
        return "January";
    case 2:
        System.out.println("February");
        return "February";
    case 3:
        System.out.println("March");
        return "March";
    case 4:
        System.out.println("April");
        return "April";
    case 5:
        System.out.println("May");
        return "May";
    case 6:
        System.out.println("June");
        return "June";
    case 7:
        System.out.println("July");
        return "July";
    case 8:
        System.out.println("August");
        return "August";
    case 9:
        System.out.println("September");
        return "September";
    case 10:
        System.out.println("October");
        return "October";
    case 11:
        System.out.println("November");
        return "November";
    case 12:
        System.out.println("December");
        return "December";
    // for any number less than 1 and greater than 12 throw an exception
    default:
        throw new RuntimeException("Invalid month number");
    }
}

Your entire workflow has 2 print statements getting executed.您的整个工作流程有 2 个打印语句正在执行。 One inside getMonth() method(which is printing "MAY"),and other in main() method(which prints 5)一个在getMonth()方法中(打印“MAY”),另一个在main()方法中(打印 5)

What you need to do is change the return type of getMonth() from int to String您需要做的是将getMonth()的返回类型从int更改为String

 public String getMonth() {

    switch (month) {

      case 1: return "January";

      case 2: return "February";

      case 3: return "March";

      case 4: return "April";

      case 5: return "May";

      case 6: return "June";

      case 7: return "July";

      case 8: return "August";

      case 9: return "September";

      case 10: return "October";

      case 11: return "November";

      case 12: return "December";

        }

        return "Invalid Month";

}

This way, your workflow is going to have a single print statement in main method.这样,您的工作流程将在 main 方法中有一个打印语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM