欢迎访问宙启技术站
智能推送

Java中如何调用print函数输出文本

发布时间:2023-10-28 09:20:26

在Java中,可以使用System.out.print()System.out.println()System.out.printf()方法来输出文本。

1. System.out.print()方法用于输出字符串,并将光标保持在同一行。可以将多个字符串使用+连接起来进行输出。

System.out.print("Hello");
System.out.print("World");

输出结果为:HelloWorld

2. System.out.println()方法用于输出字符串,并在最后添加一个换行符。同样可以将多个字符串使用+连接起来进行输出。

System.out.println("Hello");
System.out.println("World");

输出结果为:

Hello

World

3. System.out.printf()方法用于格式化输出。可以使用格式化字符串来定义输出的格式,如%s表示字符串,%d表示整数,%f表示浮点数等。可以将格式化字符串中的格式符和要输出的内容进行匹配。

String name = "Alice";
int age = 20;
System.out.printf("My name is %s and I am %d years old.", name, age);

输出结果为:My name is Alice and I am 20 years old.

以上是Java中常用的输出文本的方式。根据具体需求,选择适合的方法即可。