提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
此方法以ASCII艺术形式创建小的状态栏,以便在控制台或日志文件中进行打印。
//package com.nowjava;/*来 自 N o w J a v a . c o m*/ public class Main { public static void main(String[] argv) throws Exception { int length = 2; double d = 2.45678; boolean showPercentage = true; System.out.println(createPercentageBar(length, d, showPercentage)); } /** * This method creates small status bars in ASCII art for printing in the * console or in log files. The status bar looks like "[********.........]" * without shown percentage or "[*******........] ( 45%)" with percentage * shown. * * @param length * is the total length of the status bar. In this length the * brackets are included. The percentage number is extra length. * @param d * is the percentage to show. * @param showPercentage * defines whether the percentage value is to be shown. * @return A String is returned containing the ASCII art representation of * the string */ public static String createPercentageBar(int length, double d,/**来自 时 代 Java - nowjava.com**/ boolean showPercentage) { StringBuffer buffer = new StringBuffer("["); int starNum = (int) Math.round(d * (length - 2)); for (int dummy = 1; dummy <= length - 2; dummy++) { if (dummy <= starNum) { buffer.append('*'); } else { buffer.append('.'); } } buffer.append(']'); if (showPercentage) { buffer.append(" ("); StringBuffer num =