Results 1 to 5 of 5

Thread: Making triangle

  1. #1
    banie is offline Member
    Join Date
    Jan 2008
    Posts
    21
    Rep Power
    0

    Default Making triangle

    Hi,

    I just wanna do triangle. The output is like this:

    @
    @@
    @@@
    @@@@
    @@@@@
    @@@@@@
    @@@@@@@
    @@@@@@@

    Here is my code:

    Java Code:
    public class Banie {
    
    private final int height;
    
    private final int width;
    
    public Banie(final int height, final int width) { // Constructor
    this.height = height;
    this.width = width;
    }
    
    public void print() {
    for (int line = 0; line < height; line++) {
    final double percentage = line / (double) height;
    final int item = (int) (percentage * width) + 1;  // Type casting
    for (int at = 0; at < item; at++) {
    
    System.out.print("@");
    }
    
    System.out.println();
    if ( height == item);
    }
    
    }
    
    public static void main(String[] args) {
    Banie t = new Banie(7, 7); // Parsing args
    t.print(); // Calling method
    }
    }

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Okay

    Hello banie :D

    I indented and tested your code and it looks file:
    Java Code:
    public class Banie {
    	private final int height;
    	private final int width;
    
    	public Banie(final int height, final int width) { // Constructor
    		this.height = height;
    		this.width = width;
    	}
    
    	public void print() {
    		for (int line = 0; line < height; line++) {
    			final double percentage = line / (double) height;
    			final int item = (int) (percentage * width) + 1;  // Type casting
    			for (int at = 0; at < item; at++) {
    				System.out.print("@");
    			}
    			System.out.println();
    			if ( height == item);
    		}
    	}
    }
    It gives output
    Java Code:
    @
    @@
    @@@
    @@@@
    @@@@@
    @@@@@@
    @@@@@@@
    Is this what you wanted? Or, do you need:
    Java Code:
       @
      @@@
     @@@@@
    @@@@@@@
    Last edited by tim; 01-30-2008 at 09:57 AM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    banie, this topic was discussed recently... you may wish to use the search feature next time. When you use the search, compare your code to the code you find from others. This helps to strengthen your coding "eyes" and reasoning ability/logic skills.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    banie is offline Member
    Join Date
    Jan 2008
    Posts
    21
    Rep Power
    0

    Default

    The output that i want is like this:

    @
    @@
    @@@
    @@@@
    @@@@@
    @@@@@@
    @@@@@@@
    @@@@@@@

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by banie View Post
    The output that i want is like this:

    @
    @@
    @@@
    @@@@
    @@@@@
    @@@@@@
    @@@@@@@
    @@@@@@@
    So what exactly is the problem? tim provided you with an easy solution, now just add that extra line in and you've got that output.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. change the square to triangle java
    By anotsu in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 12:17 PM
  2. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 03:08 PM
  3. Replies: 0
    Last Post: 04-12-2008, 09:39 PM
  4. Help with making this algorithm better
    By RLRExtra in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 05:11 PM
  5. Making a session in swing
    By sandor in forum AWT / Swing
    Replies: 3
    Last Post: 04-22-2007, 11:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •