print* output formatted notes

println

public class Main{ public static void main(String[] args) { for(int i=1;i<10;i++){ for(int j=1;j<=i;j++){ System.out.printf( "%2d%2s%2d%2s%3d",j," ",i,"=",j i," ");//The output has both integer type and character type, } System.out.println (" "); }At this time, printf will report an error, The method printf(Locale, String, Object[]) in the type PrintStream is not applicable for the arguments (String, int, String, int, String, int, String)







The default compatible version of Java in Eclipse is 1.4, just change it to 1.5 and above. Project "Properties" Java complier "complier compliance lever: 1.5

// "%" means formatted output, and the content after "%" is the definition of the format.
System.out.printf("%f", f);// "f" means formatted output floating point number.
System.out.println();
System.out.printf("%9.2f", f);// The 9 in "9.2" represents the length of the output, and 2 represents the number of digits after the decimal point.
System.out.println();
System.out.printf("%+9.2f", f);// "+" means that the output number is signed.
System.out.println();
System.out.printf("%-9.4f", f);// "-" means that the output numbers are aligned to the left (the default is right alignment).
System.out.println();
System.out.printf("%±9.3f", f);// "±" means that the output number is signed and left-justified.
System.out.println();
System.out.printf("%d", d);// "d" means output decimal integer.
System.out.println();
System.out.printf("%o", d);// "o" ​​means output octal integer.
System.out.println();
System.out.printf("%x", d);// "x" means output hexadecimal integer.
System.out.println();
System.out.printf("%#x", d);// "#x" means output an integer with hexadecimal notation.
System.out.println();
System.out.printf("%#o", d);// "#o" means output an integer with octal sign.
System.out.println();
System.out.printf("%s", str);// "s" means output string.
System.out.println();
// You can output multiple variables, pay attention to the order.
System.out.printf("Output a floating point number: %f, an integer: %d, a string: %s", f, d, str);
System.out.println();
System.out.printf( "String: %2 s, s, %1The hexadecimal number of s and d: %1KaTeX parse error: Expected'EOF', got'#' at position 1: #̲x", d, str);…"Indicates the number of variables.

Reference: https://blog.csdn.net/qq_32776913/article/details/74331791?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160251170619726892412773%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522% 257D&request_id=160251170619726892412773&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2 all first_rank_ecpm_v3~pc_rank_v2-1-74331791.first_rank_ecpm_v3_pc_rank_v2&utm_87%BC_rank_v2&utm_87%BA%E5%E5%E8%BE%println%E5% BC%8F&spm=1018.2118.3001.4187


Keep n decimal places

double d = 345.678;

System.out.printf("%7.2f",d); //7 means the length of the output, 2 means the number of digits after the decimal point

Keep 1 decimal place, the length is not limited: System.out.printf("%.1f",d); //1 means the number of digits after the decimal point, and no number before the decimal point means unlimited length

or:

System.out.println(String.format("%.2f",x));

Reference:
https://blog.csdn.net/zjhzcjg/article/details/78389952?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160251128219195240431876%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255 .%2522%257D&request_id=160251128219195240431876&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2 all first_rank_ecpm_v3~pc_rank_v2-2-78389952.first_rank_ecpm_v3_pc_5%E5%Eterm=java+%E5%E5%C0%BC 96%E8%BE%93%E5%87%BA%E6%8C%87%E5%AE%9A%E9%95%BF%E5%BA%A6%E5%B0%8F%E6%95%B0&spm= 1018.2118.3001.4187


Guess you like

Origin blog.csdn.net/DAurora/article/details/109038196