5 Replies - 190 Views - Last Post: 18 December 2013 - 04:40 AM Rate Topic: -----

#1 mike8629  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-December 13

Method printing

Posted 18 December 2013 - 03:48 AM

How would I do this so the method prints out the amount of stars given in the parameters?
Would I use a loop?


Create a method printStars that prints the given amount of stars and a line break.

Create the method in the following body:

private static void printStars(int amount) {
    // you can print one star with the command
    // System.out.print("*");
    // call this command amount times
}

public static void main(String[] args) {
    printStars(5);
    printStars(3);
    printStars(9);
}


The program must output:
*****
***
*********


Is This A Good Question/Topic? 0
  • +

Replies To: Method printing

#2 ChrisNt  Icon User is online

  • D.I.C Addict

Reputation: 198
  • View blog
  • Posts: 576
  • Joined: 31-July 13

Re: Method printing

Posted 18 December 2013 - 03:59 AM

Yes use a for loop..

Link
Was This Post Helpful? 1
  • +
  • -

#3 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 509
  • Joined: 07-March 09

Re: Method printing

Posted 18 December 2013 - 04:13 AM

Take a look at Apache's common-lang StringUtils.repeat(String str, int repeat)

commons-lang said:

repeat

public static String repeat(String str, int repeat)

Repeat a String repeat times to form a new String.

StringUtils.repeat(null, 2) = null
StringUtils.repeat("", 0) = ""
StringUtils.repeat("", 2) = ""
StringUtils.repeat("a", 3) = "aaa"
StringUtils.repeat("ab", 2) = "abab"
StringUtils.repeat("a", -2) = ""


Parameters:
str - the String to repeat, may be null
repeat - number of times to repeat str, negative treated as zero

Returns:
a new String consisting of the original String repeated, null if null String input


You can see how they do it here: Source
Was This Post Helpful? 1
  • +
  • -

#4 mike8629  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-December 13

Re: Method printing

Posted 18 December 2013 - 04:30 AM

	public static void main(String[] args) {
		printStars(5);
		printStars(3);
		printStars(9);
	}
	
	private static void printStars(int amt) {

		for(int i = 0;i < amt; i++){
			System.out.print("*");

	    }
	}


It outputs 1 line of stars, How would I make it so it would make a new line between each time the method is used.
Was This Post Helpful? 0
  • +
  • -

#5 ChrisNt  Icon User is online

  • D.I.C Addict

Reputation: 198
  • View blog
  • Posts: 576
  • Joined: 31-July 13

Re: Method printing

Posted 18 December 2013 - 04:38 AM

outside your for loop print an empty line..

System.out.println();
Was This Post Helpful? 1
  • +
  • -

#6 mike8629  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-December 13

Re: Method printing

Posted 18 December 2013 - 04:40 AM

View PostChrisNt, on 18 December 2013 - 04:38 AM, said:

outside your for loop print an empty line..

System.out.println();

Argh, it all becomes apparent once someone says it.
Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1