Java printf Usage Guide

The printf method in Java is used to format output content to the console. Its usage is similar to the printf method in the C language. Here is the basic usage of the printf method:

System.out.printf("格式字符串", 参数1, 参数2, ...);

The format string contains the formatting specifications for the content to be output, while parameters such as parameter1, parameter2, etc. are the actual values that will replace the placeholders in the format string.

For example, you can use %s to represent strings, %d to represent integers, %f to represent floating point numbers, and so on. Here is an example:

String name = "Alice";
int age = 30;
double height = 1.75;

System.out.printf("姓名:%s,年龄:%d,身高:%.2f米", name, age, height);

The output result is:

姓名:Alice,年龄:30,身高:1.75米

The printf method allows for more flexibility in controlling the formatting of output, resulting in clearer and more readable content.

bannerAds