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
- You can use
print
instead ofprintln
to write part of the line. In your case, you can doSystem.out.print("A ")
to append#
to it later. - 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 - 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. - 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.
精彩评论