Java self - numeric and string formatting output

Java using printf or format to format output

Step 1: Formatting Output

If you do not use formatted output, on the need for string concatenation, if the variables are more complicated mosaic will look
to use formatted output, you can clear and concise

% s indicates a string
% d represents a number
% n represents a newline

package digit;
  
public class TestNumber {
  
    public static void main(String[] args) {
 
        String name ="盖伦";
        int kill = 8;
        String title="超神";
         
        //直接使用+进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差
        String sentence = name+ " 在进行了连续 " + kill + " 次击杀后,获得了 " + title +" 的称号";
         
        System.out.println(sentence);
         
        //使用格式化输出
        //%s表示字符串,%d表示数字,%n表示换行
        String sentenceFormat ="%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n";
        System.out.printf(sentenceFormat,name,kill,title);
         
    }
}

Step 2: printf and format

printf format and can achieve exactly the same effect, see the eclipse through java source code that
can be seen directly in the printf call format
printf and format

package digit;
  
public class TestNumber {
  
    public static void main(String[] args) {
 
        String name ="盖伦";
        int kill = 8;
        String title="超神";
         
        String sentenceFormat ="%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n";
        //使用printf格式化输出
        System.out.printf(sentenceFormat,name,kill,title);
        //使用format格式化输出
        System.out.format(sentenceFormat,name,kill,title);
         
    }
}

Step 3: newline

Newline character is a separate line --- '\ n' newline (newline)
carriage return is back to the beginning of the line --- '\ r' carriage return (return)
knock a carriage return at the eclipse, in fact, is back car newline
Java is cross-platform programming language, the same code can be used in different platforms, such as Windows, Linux, Mac
but on different operating systems, line breaks are not the same as
in DOS and Windows (1), at the end of each line is "\ r \ the n-";
(2) Linux system, the end of each line only "\ the n-";
(3) Mac system, at the end of each line is only "\ r".
In order to make a line break with a java program on all operating systems have the same performance, use% n, you can do the wrap platform-independent

package digit;
  
public class TestNumber {
  
    public static void main(String[] args) {
 
        System.out.printf("这是换行符%n");
        System.out.printf("这是换行符%n");
         
    }
}

Step 4: total length, left, up 0, thousands separator, decimal places, localized expression of
other conventional manner formatted

The total length, left, up 0, thousands separator, decimal places, localized expression

package digit;
  
import java.util.Locale;
   
public class TestNumber {
   
    public static void main(String[] args) {
        int year = 2020;
        //总长度,左对齐,补0,千位分隔符,小数点位数,本地化表达
          
        //直接打印数字
        System.out.format("%d%n",year);
        //总长度是8,默认右对齐
        System.out.format("%8d%n",year);
        //总长度是8,左对齐
        System.out.format("%-8d%n",year);
        //总长度是8,不够补0
        System.out.format("%08d%n",year);
        //千位分隔符
        System.out.format("%,8d%n",year*10000);
  
        //小数点位数
        System.out.format("%.2f%n",Math.PI);
          
        //不同国家的千位分隔符
        System.out.format(Locale.FRANCE,"%,.2f%n",Math.PI*10000);
        System.out.format(Locale.US,"%,.2f%n",Math.PI*10000);
        System.out.format(Locale.UK,"%,.2f%n",Math.PI*10000);
          
    }
}

Exercise : Huang

Scanner data string read by, and then outputs the formatted with the following

Wenzhou, Zhejiang largest tanneries south tanneries closed down, son of a bitch boss Huang Chihepiaodu, owed 3.5 billion, with his sister ran! We have no way, holding the purse arrived wages! Price is Yi Bai more than two Bai more than three Bai more wallet , and now all sold only twenty, as long as all twenty! Huang son of a bitch, you're not human! we worked hard to give you six months, you do not wages you also my hard-earned money, but also my hard-earned money!

The answer :

package digit;
 
import java.util.Locale;
import java.util.Scanner;
 
public class TestNumber {
 
    public static void main(String[] args) {
 
        Scanner s = new Scanner(System.in);
        System.out.println("请输入地名:");
        String location = s.nextLine();
        System.out.println("请输入公司类型:");
        String companyCategory = s.nextLine();
        System.out.println("请输入公司名称:");
        String companyName = s.nextLine();
        System.out.println("请输入老板名称:");
        String bossName = s.nextLine();
        System.out.println("请输入金额:");
        String money = s.nextLine();
        System.out.println("请输入产品:");
        String product = s.nextLine();
        System.out.println("请输入价格计量单位:");
        String unit = s.nextLine();
 
        String model = "%s最大%s%s倒闭了,王八蛋老板%s吃喝嫖赌,欠下了%s个亿,\r\n"
                + "带着他的小姨子跑了!我们没有办法,拿着%s抵工资!原价都是一%s多、两%s多、三%s多的%s,\r\n"
                + "现在全部只卖二十块,统统只要二十块!%s王八蛋,你不是人!我们辛辛苦苦给你干了大半年,\r\n"
                + "你不发工资,你还我血汗钱,还我血汗钱!";       
        System.out.format(model, location,companyCategory,companyName,bossName,money,product,unit,unit,unit,product,bossName);
         
    }
}

Guess you like

Origin www.cnblogs.com/jeddzd/p/11611806.html