console java
控制台类printf()方法 (Console Class printf() method)
printf() method is available in java.io package.
printf()方法在java.io包中可用。
printf() method is used to print a formatted string to this Console with the help of the given format string and parameters.
printf()方法用于在给定的格式字符串和参数的帮助下,将格式化的字符串打印到此控制台。
printf() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
printf()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
printf() method may throw an exception at the time of formatting this Console.
在格式化此控制台时, printf()方法可能会引发异常。
IllegalFormatException: This exception may throw when the given string format is not valid.
IllegalFormatException :如果给定的字符串格式无效,则可能引发此异常。
Syntax:
句法:
public Console printf(String frmt , Object... params);
Parameter(s):
参数:
String frmt – represents the format string as defined in the Format string.
字符串frmt –表示格式字符串中定义的格式字符串。
Object... params – represents the parameters addressed by the format specifiers in the format string.
对象...参数 –表示格式字符串中格式说明符所寻址的参数。
Return value:
返回值:
The return type of the method is Console, it returns this Console.
方法的返回类型为Console ,它将返回此Console。
Example:
例:
// Java program to demonstrate the example
// of Console printf(String frmt , Object… params)
// method of Console
import java.io.*;
public class PrintFOfConsole {
public static void main(String[] args) {
try {
// Instantiates Console
Console con = System.console();
// Define a string format
String frmt = "%1$5s %2$20s %3$30s%n";
// By using printf() method isto print the
// the formatted given string
con.format(frmt, "S_Name", "S_Add", "S_ph");
con.format(frmt, "Preeti", "Delhi", "9425667852");
con.format(frmt, "Rahul", "Delhi", "8871459682");
con.format(frmt, "Shreya", "Delhi", "7216589745");
con.format(frmt, "Rama", "Bangalore", "8871568492");
con.format(frmt, "Anjali", "London", "8872589261");
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
Output
输出量
S_Name S_Add S_ph
Preeti Delhi 9425667852
Rahul Delhi 8871459682
Shreya Delhi 7216589745
Rama Bangalore 8871568492
Anjali London 8872589261
翻译自: https://www.includehelp.com/java/console-printf-method-with-example.aspx
console java