String方法的返回值

时间:2013-09-26 02:06:33

标签: java

我刚刚开始使用java,因此我正在编写一些简单的程序来学习基础知识。我想知道我的takeNames方法使用什么返回类型?我知道它必须返回一个字符串,所以我使用“”,以便用户看不到任何东西。

这是我的第一个程序之一,所以我认为我必须做错事,或者可能没有错,但我的代码肯定会更有效率。

import java.util.Scanner;

public class Name{

  String fName;
  String lName;
  String line = takeNames(fName, lName);

  public String takeNames(String fName, String lName){

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    fName = scanner.next();
    lName = scanner.next();
    System.out.printf("Hello %s %s", fName, lName);
    return "";
  }


  public static void main(String [] args){
    Name l = new Name();
    System.out.println(l.line);


  }
}

有人能告诉我,我用于退货声明的是否可以接受?或者有人能告诉我一个更好的方式来编写我的代码吗? 非常感谢!

6 个答案:

答案 0 :(得分:1)

如果您不想返回任何内容,请返回类型void -

public void takeNames(String fName, String lName){

Scanner scanner = new Scanner(System.in);
System.out.println("Enter your first and last name");
fName = scanner.next();
lName = scanner.next();
System.out.printf("Hello %s %s", fName, lName);//note no return statement needed!


}

答案 1 :(得分:1)

你不需要退货。

public void takeNames(String fName, String lName){

这样称呼(用此替换你的main方法):

Name l = new Name();
l.takeNames();

删除该行:

String line = takeNames(fName, lName);

然后没用。

如果您想使用System.out.println(l.line),请尝试:

Scanner scanner = new Scanner(System.in);
System.out.println("Enter your first and last name");
fName = scanner.next();
lName = scanner.next();
return "Hello " + fName + " " + lName;
// can also be String.format("Hello %s %s", fName, lName);

答案 2 :(得分:1)

您稍后使用System.out.println(l.line)打印此方法的返回值,因此您可能希望返回在此方法中打印出来的字符串,而不是将其打印出来。

public String takeNames(String fName, String lName){

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    fName = scanner.next();
    lName = scanner.next();
    return String.format("Hello %s %s", fName, lName);
  }

答案 3 :(得分:1)

package com.linkage.test;
import java.util.Scanner;

public class Name{


  public static String takeNames(){

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    String fName;
    String lName;
    fName = scanner.next();
    lName = scanner.next();

    System.out.printf("Hello %s %s", fName, lName);
    return fName+"_"+lName;
  }


  public static void main(String [] args){
      String[] arrays = takeNames().split("_");
      System.out.println();
      for (int i = 0; i < arrays.length; i++) {
          System.out.println(arrays[i]);
      }
  }
}

答案 4 :(得分:1)

return语句的目的是返回稍后会有用的内容。除非打印出空白行(如System.out.println(l.line);所做的那样)对您有用,否则执行该return语句是没有意义的。

更大的问题是您的代码结构。将其更改为:

String fName;
String lName;
//Get rid of line

public void takeNames(){ //You don't need fName or lName in this line, since they're part of the object's fields
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    fName = scanner.next();
    lName = scanner.next();
    System.out.printf("Hello %s %s", fName, lName);
}


public static void main(String [] args){
    Name l = new Name();
    //What should you call here to get the same effect as before? Will let you figure that out
}

答案 5 :(得分:1)

更好的方法是

import java.util.Scanner;

public class Name{

  String fName;
  String lName;
  String fullName;

  public String takeNames(){ // don't need parameters else it will shadow above variables   

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    fName = scanner.next();
    lName = scanner.next();

    return fName + lName; // return full name 
  }


  public static void main(String [] args){
    Name l = new Name();
    fullName= l.takeNames(); // assign fullName returned from takeNames()
    System.out.println(fullName); // print


  }
}