开发者

java + print vertical BAR

I start today to write my first java program I write java program that count capital letters from args[0] and display it by number

开发者_运维问答
     charCountA = args[0].replaceAll("[^A]", "").length();
     charCountB = args[0].replaceAll("[^B]", "").length();
     charCountC = args[0].replaceAll("[^C]", "").length();

               System.out.println("A   " + charCountA );
               System.out.println("B   " + charCountB );
               System.out.println("C   " + charCountC );

if args[0]=ABBCCC then the program print

    A 1

my question how to display the charCountA and other cahrCountB/C.... by vertical bar for example

 charCountA=1 then will print --> A  #
 charCountA=2 then will print --> A  ##
 charCountA=3 then will print --> A  ###
 charCountA=3 then will print --> A  ####

and so on


  1. You can use print instead of println to write part of the line. In your case, you can do System.out.print("A ") to append # to it later.
  2. You can use for operator to loop some specific amount of iterations doing same action each iteration. In your case, you would need something like: for (int i=0;i<charCountA;i = i + 1). You can read more about for loop here: Java tutorials: The for Statement
  3. In each iteration you would need to output one # sign. So if repeated charCountA times - you would get a horizontal bar. So simply insert System.out.print("#") into the loop body.
  4. After writing the bar, you would need to go to the new line to print next letter. You can do that by writing System.out.println() without any parameters.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜