12 Replies - 19028 Views - Last Post: 07 March 2009 - 05:32 PM Rate Topic: -----

#1 6moose  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-March 09

Java asterisk triangle

Posted 05 March 2009 - 06:02 PM

This question has been asked before but the results needed are different. I just need to find a way to reverse this. and to be completely honest i need this code explained to me.

the results i need are like this...
user input...4
*
**
***
****
***
**
*
This code does half of the needed result.
any help would be great, thanks in advance.

 import java.util.*;

public class Triangle
{
	public static void main(String[]args)
	{
		int n = 0;
		int spaces = n;
		int ast;

		System.out.println("Please enter a number and i will draw a triangle with these *");
		Scanner keyboard = new Scanner(System.in);
		n = keyboard.nextInt();

		 for(int i = 0; i < n; i++)

		 {
			ast = 2*i+1;
			for(int j = 1; j <= spaces+ast; j++)
			{
				if(j <= spaces)
					System.out.print(' ');
				else
					System.out.print('*');
			}
			System.out.println();
			spaces--;
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java asterisk triangle

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Java asterisk triangle

Posted 05 March 2009 - 06:05 PM

this code is mine :P, i wrote it and posted it in another thread..

This post has been edited by mostyfriedman: 05 March 2009 - 06:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 6moose  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-March 09

Re: Java asterisk triangle

Posted 05 March 2009 - 06:09 PM

View Postmostyfriedman, on 5 Mar, 2009 - 05:05 PM, said:

this code is mine :P, i wrote it and posted it in another thread..


Yes your right I will not deny it. I am just trying to understand it. Sorry i did not give you credit.
Was This Post Helpful? 0
  • +
  • -

#4 6moose  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-March 09

Re: Java asterisk triangle

Posted 06 March 2009 - 02:58 PM

*bump* Can someone please help me understand this code. and how i would have it draw the other half of the triangle? Mainly the "for" statements.

This post has been edited by 6moose: 06 March 2009 - 02:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Java asterisk triangle

Posted 06 March 2009 - 03:21 PM

If you are trying to understand it then I would write out the for statement on paper each time through the loop to get an idea of what is happening.
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Java asterisk triangle

Posted 06 March 2009 - 04:03 PM

you do realize that this algorithm draws a different shape than the one you want
Was This Post Helpful? 0
  • +
  • -

#7 6moose  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-March 09

Re: Java asterisk triangle

Posted 07 March 2009 - 02:10 PM

Yes I did realize this. I thought I could modify the code to where it would give me the other half of the triangle. However I am still lost.
Was This Post Helpful? 0
  • +
  • -

#8 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Java asterisk triangle

Posted 07 March 2009 - 02:14 PM

Well DIC has this policy:

[rules][/rules]

So you will have to attempt the problem first, and copying and pasting the existing code from a different topic does not count as effort on your problem
Was This Post Helpful? 0
  • +
  • -

#9 6moose  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-March 09

Re: Java asterisk triangle

Posted 07 March 2009 - 02:19 PM

The code is in the first post of this thread. I am not asking you to do my homewrk, just point me in the right direction

This post has been edited by 6moose: 07 March 2009 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#10 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Java asterisk triangle

Posted 07 March 2009 - 02:22 PM

The code in the first post of this thread is from a different thread, and does not print the shape you wish.

My advice still stands: write out each of the outputs from the loop on paper step by step if you are having problems seeing what is happening.
Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Java asterisk triangle

Posted 07 March 2009 - 02:43 PM

well the above algorithm which I wrote , prints an isosceles triangle, what you need to do is remove the spaces and reverse the triangle to get the other half

This post has been edited by mostyfriedman: 07 March 2009 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#12 6moose  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-March 09

Re: Java asterisk triangle

Posted 07 March 2009 - 05:19 PM

Hopefully you will be relived to know I got it. Thank you for your patience. And mostyfriedman you will be glad to know that is my own code now I did it from the ground up.
Was This Post Helpful? 0
  • +
  • -

#13 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Java asterisk triangle

Posted 07 March 2009 - 05:32 PM

View Post6moose, on 7 Mar, 2009 - 04:19 PM, said:

Hopefully you will be relived to know I got it. Thank you for your patience. And mostyfriedman you will be glad to know that is my own code now I did it from the ground up.


good, i don't really care if you copied it..just hope you learned something
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1