JAVA-Numbers and strings (four) formatted output

Formatted output

Syntax: printf (formatting statement, variable name)

	public static void main(String args[]) {
    
    
		String a = "明天星期%s";
		String b = "三";
		System.out.printf(a,b);
	}
}

printf and format can achieve exactly the same effect

		String a = "明天星期%s";
		String b = "三";
		System.out.format(a,b);

Exercise-Yellow Crane

import java.util.Scanner;
public class TestNumber {
    
    
	public static void main(String args[]) {
    
    
		Scanner s = new Scanner(System.in);
		System.out.println("请输入公司名字:");
		String a = s.next();
		System.out.println("请输入老板名字:");
		String b = s.next();
		System.out.println("请输入欠下的赌债:");
		float c = s.nextFloat();

		
		
		
		
		String item = "浙江温州最大皮革厂%s倒闭了,王八蛋老板%s吃喝嫖赌,欠下了%f个亿";
		System.out.format(item,a,b,c);
		
		
	}
}

Insert picture description here

The method of reserved digits
uses "%.2f" in the format statement

"欠下了%.2f个亿"

Guess you like

Origin blog.csdn.net/qq_17802895/article/details/108714126