Java:printf 语句中的文字百分号 [英] Java: Literal percent sign in printf statement

查看:25
本文介绍了Java:printf 语句中的文字百分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Java 中的 printf 语句中添加一个实际的百分号,但出现错误:

I'm trying to add an actual percent sign into a printf statement in Java and I'm getting the error:

lab1.java:166: illegal escape character
                System.out.printf("%s	%s	%1.2f\%	%1.2f\%
",ID,pattern,support,confidence);
                                                 ^
lab1.java:166: illegal escape character
                System.out.printf("%s	%s	%1.2f\%	%1.2f\%
",ID,pattern,support,confidence);
                                                          ^
2 errors

我不知道如何将实际百分号放入我的 printf 中?我认为使用 \% 来逃避它会起作用,但事实并非如此.

I can't figure out how to put an actual percent sign into my printf? I thought using \% to escape it would work, but it isn't.

有什么想法吗?

推荐答案

百分号使用百分号转义:

The percent sign is escaped using a percent sign:

System.out.printf("%s	%s	%1.2f%%	%1.2f%%
",ID,pattern,support,confidence);

完整语法 可以在 java 文档 中访问.此特定信息位于第一个链接的 Conversions 部分.

The complete syntax can be accessed in java docs. This particular information is in the section Conversions of the first link.

编译器产生错误的原因是只有有限数量的characters 可以跟在反斜杠后面.% 不是有效字符.

The reason the compiler is generating an error is that only a limited amount of characters may follow a backslash. % is not a valid character.

这篇关于Java:printf 语句中的文字百分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆