gpt4 book ai didi

java - 非法文本 block 打开定界符序列,缺少行终止符

转载 作者:行者123 更新时间:2023-11-29 04:07:16 24 4
gpt4 key购买 nike

Java 13 来了,所以我开始研究它的新特性,其中之一就是文本 block

我写了一个简单的程序

public final class Example {
public static void main(String[] args) {
final String greeting = """Hello
It's me, Andrew!""";
System.out.println(greeting);
}
}

我期待看到

Hello
It's me, Andrew!

我得到的是一个编译错误说

illegal text block open delimiter sequence, missing line terminator

最佳答案

文本 block 的上下文必须从一个新行开始。

public final class Example {
public static void main(String[] args) {
final String greeting = """
Hello
It's me, Andrew!""";
System.out.println(greeting);
}
}

打印

Hello
It's me, Andrew!

摘自 JEP 355: Text Blocks (Preview) :

A text block consists of zero or more content characters, enclosed by opening and closing delimiters.

The opening delimiter is a sequence of three double quote characters (""") followed by zero or more white spaces followed by a line terminator. The content begins at the first character after the line terminator of the opening delimiter.

不过,您不一定非要在内容末尾放置行终止符。

The closing delimiter is a sequence of three double quote characters. The content ends at the last character before the first double quote of the closing delimiter.

final String greeting = """
Hello
It's me, Andrew!
""";

意味着

Hello
It's me, Andrew!
<an empty line here>

我发现它非常不清楚,所以我不得不与社区分享。

关于java - 非法文本 block 打开定界符序列,缺少行终止符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57853217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com