Java 如何仅打印出方法的返回值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20858833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:50:19  来源:igfitidea点击:

How to print out only return value of a method?

java

提问by v_7

I've got a homework that I should write a couple of methods(e.g. a method that prompts customer for the car type and returns a valid car type) and then my program should display the car type, number of days car rented for,extras etc. This is first method that teacher wanted me to write ,

我有一个作业,我应该写一些方法(例如,提示客户输入汽车类型并返回有效汽车类型的方法),然后我的程序应该显示汽车类型,汽车租赁天数,额外的等等 这是老师要我写的第一种方法,

public static String promptForCarType(){
        Scanner input = new Scanner(System.in);
        char type;
        System.out.println("(E) Economy  - 50 TL");
        System.out.println("(M) Midsize  - 70 TL");
        System.out.println("(F) Fullsize - 100 TL");
        do{
            System.out.println("Enter the car type (E/M/F) : ");
            type = input.next().charAt(0);
            type = Character.toUpperCase(type);
        } while (type != 'E' && type != 'M' && type != 'F' ); //I tried to define condition with "||" operator,didn't work.


        switch(type) {
            case 'E' : ;return "Economy";             
            case 'M' : return "Midsize";
            case 'F' : return "Fullsize";
            default : return " ";

        }


    }

How can I print out only return value of this method ? Should I add System.out.println("Car type is ...) part in promptForCarType()?

如何仅打印出此方法的返回值?我应该在 promptForCarType() 中添加 System.out.println("Car type is ...) 部分吗?

采纳答案by Adarsh

The code control returns to the invoking function when it encounters the keyword return. So you can only operate in the current method till the program reaches the keyword return. So, if you need to print something, print it before the return. In your case, you need to print the value in your switch-case construct , for each case before the return statement.

代码控件在遇到关键字return时返回到调用函数。所以只能在当前方法中操作,直到程序到达关键字return。因此,如果您需要打印某些内容,请在返回之前打印。在您的情况下,您需要在 return 语句之前为每种情况打印 switch-case 构造中的值。

 switch(type) {
        case 'E' : System.out.println("Economy");
                   return "Economy";             
       // similarly for each switch case.
    }

Or, the better way would be to assign the car type to a variable of type String and then print the value. and write a common return statement for the entire method(for all cases).

或者,更好的方法是将汽车类型分配给 String 类型的变量,然后打印该值。并为整个方法编写一个通用的 return 语句(适用于所有情况)。

   String carType;
   switch(type) {
        case 'E' : carType ="Economy";
       // similarly for each switch case.
    }
    System.out.println(carType);
    return carType;

回答by A4L

  1. Call your method from your main method
  2. Assign the retrurned value to a variable
  3. print that variable

    String carType = promptForCarType(); //1 + 2
    System.out.println(carType); //3
    
  1. 从你的主方法调用你的方法
  2. 将返回的值分配给变量
  3. 打印那个变量

    String carType = promptForCarType(); //1 + 2
    System.out.println(carType); //3
    

Or simply

或者干脆

System.out.println(promptForCarType());

回答by user3149556

if you want to print the instance of the car type you would simply go to your main method and print the called instance of your car accessing the method promptForCar. Ex: System.out.println(object.method);

如果您想打印汽车类型的实例,您只需转到主方法并打印访问方法 promptForCar 的汽车的调用实例。例如: System.out.println(object.method);

回答by Babbleshack

String carType = promptForCarType();
System.out.println("Car type is: " + carType);

should work. the return value of promptForCar() is stored in the string carType, and then the print statement uses the string variable to print the return value. `

应该管用。promptForCar() 的返回值存储在字符串carType 中,然后print 语句使用字符串变量打印返回值。`

回答by Elliott Frisch

You could just return from an infinite loop (and simplify your code) like so -

您可以像这样从无限循环中返回(并简化您的代码) -

public static String promptForCarType() {
    Scanner input = new Scanner(System.in);
    char type;
    for (;;) {
        System.out.println("(E) Economy  - 50 TL");
        System.out.println("(M) Midsize  - 70 TL");
        System.out.println("(F) Fullsize - 100 TL");
        System.out.println("Enter the car type (E/M/F) : ");
        type = input.next().charAt(0);
        type = Character.toUpperCase(type);
        switch (type) {
        case 'E':
            return "Economy";
        case 'M':
            return "Midsize";
        case 'F':
            return "Fullsize";
        }
    }
}