//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(" (");
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。