12 Replies - 15086 Views - Last Post: 11 April 2011 - 10:01 AM Rate Topic: -----

#1 newkid_onthe_block  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 11

creating a basic triangle using asterisks

Posted 10 April 2011 - 02:34 PM

I am trying to design a program as practice for an upcoming exam I have. I am trying to make a basic triangle using asterisks. I am fairly new to Java and I'm not sure how to get started however, I know I need to use nested loops. I want to get user input for the size of the triangle. For example, if the user inputs 3, the triangle would look like this

*
**
***
**
*

I would appreciate any advice or tips.. Thank you!!

This is what I have so far its not much but I'll post below..

/** This program takes a number from 1 to 50 from the user
 * and output asterisks in the shape of a triangle with the
 * user inputed number being the largest amount of asterisks*/

import java.util.Scanner;//Get Scanner from java utility

public class Triangle {
	public static void main(String[] args){
		
		Scanner input = new Scanner(System.in);//Create Scanner object
		
		int line; //declare the number of lines
		int numA; // declare the size of the triangle
		String ast = "*";
		
		System.out.println("Enter the size of a triangle (1 to 50): ");
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: creating a basic triangle using asterisks

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 02:48 PM

Sometimes, the triangle printed looks like:

    *
   ***
  *****
 *******
*********


rather than what you've shown, but we can work with what you have.

Describe what has to be done in words:

"If the user inputs the number 5, I have to print rows of stars that begin with one star then add another star until the user's number is reached, then decrease the number of stars printed back to one."

How many rows is that? Count the number of stars to be printed: 1 star, 2 star, 3, 4, 5, 4, 3, 2, 1 = 9. That's one less than twice the number input by the user? Will that always be the case? Seems to make sense.

So all you have to do is create a loop that prints increasing numbers of stars until the user's number is reached and then prints rows of decreasing stars until 1 star is printed.

In primitive pseudo code:

Get user's number, x
for 1 to x, x increasing by one
print a line of x stars
for x - 1 to 1, x decreasing by one
print a line of x stars


Can you code it now?

This post has been edited by GregBrannon: 10 April 2011 - 02:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 newkid_onthe_block  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 11

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:02 PM

Okay, that makes sense. How would you recommend I print increasing amount of stars until the user's inputted number? Should I use the asterisk as a String type?
Was This Post Helpful? 0
  • +
  • -

#4 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:06 PM

What do you mean by increasing amounts of stars until the user's?

Are you saying you want it to run until the user does something?
Was This Post Helpful? 0
  • +
  • -

#5 newkid_onthe_block  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 11

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:16 PM

No no.. I apologize for not being clear. Let me try and explain better. I want to have a user input an integer. That integer will designate the size of the triangle. The triangle will be made of lines of asterisks starting at 1 asterisk. At each line the amount of asterisks will increase by 1 until the number of asterisks is equal to the integer inputted by the user. The next line will decrease the asterisks by 1 and so on until the number of asterisks is back to 1.

For example, if the user input is 5 then the output would look like this:

*
**
***
****
*****
****
***
**
*


Was This Post Helpful? 0
  • +
  • -

#6 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:22 PM

Well, start by using a "for" statement until you get to the amount the user inputted.

Make sure you are using the variable++ notation.

You can see more about for statements here.
Was This Post Helpful? 0
  • +
  • -

#7 newkid_onthe_block  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 11

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 03:41 PM

Well if the user input is the variable how can I implement that into the parameter for the for loop?
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 04:13 PM

Your last question is confusing. You'll have to explain better what you don't understand.

You can print an increasing (and then decreasing number of asterisks by using a print statement without a return:

for x, x increasing by 1
{
    System.print( "*" );
}
// then print a linefeed
System.println();


This is a great opportunity to use a method, something like

public void printANumberOfAsterisks( int theNumberToPrint )

The method would print the number of asterisks specified on a single line followed by a linefeed.
Was This Post Helpful? 0
  • +
  • -

#9 newkid_onthe_block  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 11

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 04:45 PM

I'm not sure. I have yet to learn using multiple methods.

Obviously, I need to assign "*" as a String variable. I'm just confused on how to print the desired amount of asterisks per line..

/** This program takes a number from 1 to 50 from the user
 * and output asterisks in the shape of a triangle with the
 * user inputed number being the largest amount of asterisks*/

import java.util.Scanner;//Get Scanner from java utility

public class Triangle {
	public static void main(String[] args){
		
		Scanner input = new Scanner(System.in);//Create Scanner object
		
		int line; //declare the number of lines
		int numA; // declare the size of the triangle
		String ast = "*";
		
		System.out.println("Enter the size of a triangle (1 to 50): ");
		numA = input.nextInt(); //size of triangle
		
		for(line = 1; line == numA; line++){ 
		}
		
	}
}



I am really confused. I'm not sure where to progress from here. I don't know how to increase the number of asterisks per line.
Was This Post Helpful? 0
  • +
  • -

#10 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 05:00 PM

You could always Google The Java Tutorials.

Those are very helpful, so check it out.
Was This Post Helpful? -2
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: creating a basic triangle using asterisks

Posted 10 April 2011 - 10:36 PM

The condition in your for loop is when the loop should continue. When that condition becomes false, the loop will temrinate. I'm guessing you want your counter in the for loop to be <= numA, not == to it.

You will need nested loops here. The outer loop should loop from 1 through numA. The inner loop should loop from 0 through line, printing out a single "*" on each iteration. Then after the inner loop, print a new line.

You will then need a second set of loops to go backwards.

Some logic for the top-half of the triangle:
for i = 1 through numA, i++ on each iteration
     for j = 0 through i, j++ on each iteration
         print "*"
     print a new line
end for



@softwareEngineer(): Don't simply send our members back to Google. A lot of them come from Google looking for help. If a post simply warrants a "Google it" reply (as is not the case here), just report it. If there is a link you have in mind, dig it up and post it. That adds much more value to the thread. :)
Was This Post Helpful? 1
  • +
  • -

#12 newkid_onthe_block  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-March 11

Re: creating a basic triangle using asterisks

Posted 11 April 2011 - 09:32 AM

Thanks @macosxnerd101! I was able to complete the practice problem.. Thanks everyone for your help and input!
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: creating a basic triangle using asterisks

Posted 11 April 2011 - 10:01 AM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1