【问题标题】:Which of the following describes the return type and parameters of the Scanner method nextLine?以下哪项描述了 Scanner 方法 nextLine 的返回类型和参数?
【发布时间】:2020-09-25 18:19:58
【问题描述】:

问题是:下面哪个描述了Scanner方法nextLine的返回类型和参数?

我很确定 nextLine 没有参数。我不确定返回类型是 String 还是 void。是哪一个?

【问题讨论】:

标签: java


【解决方案1】:

你可以看看javadoc

public String nextLine()

public方法没有参数,返回一个String。

【讨论】:

    【解决方案2】:

    Scanner.nextLine()没有参数,返回一个String。

    考虑这段代码:

    import java.util.Scanner;
    
    public class ScannerEx {
        public static void main (String [] args) {
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            System.out.println(input);
    
        }
    }
    

    如果我输入:

    foobar
    

    它会打印出来

    foobar
    

    如果它是无效的,那么它不会返回任何东西。不仅没有输出,还会出现编译错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 2012-10-31
      相关资源
      最近更新 更多