2 Replies - 1050 Views - Last Post: 11 April 2007 - 08:03 PM Rate Topic: -----

#1 bigeyedfish9614  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-April 07

for looping

Posted 11 April 2007 - 05:23 PM

I have to create a triangle that has the height of atleast 4 and no more then 30. The triangle has to be centered at 39 spaces from the left adding 2 astericks to each row as it goes down. Right now I have just about everything done but I cannot figure out why/how to add more then one row of astericks? which is in my drawTree method. All it does right now is print out one asterick at the 39th spot. Any help would be appreciated

import java.util.*;
import javax.swing.*;

public class Tree
{
public static void main ( String [] args)
{
int size;
do
{
size = getSize();
drawTree(size);
}while(goAgain());
}
private static int getSize()
{
Scanner scanner = new Scanner(System.in);
boolean correct = false;
int height = 0;

while(correct == false)
{
System.out.print("Enter a height: ");
height = scanner.nextInt();
if(height < 4 || height > 30)
{
System.out.println("The height you entered is incorrect");
correct = false;
}
else
correct = true;
}
return height;
}
private static void drawTree(int height)
{
{

for(int count = 0; count < 39; count++)
{
System.out.print(" ");
}

System.out.print("*");

}

}





private static boolean goAgain()
{
boolean keepPlaying = true;
int selection;

while(keepPlaying)
{
selection = JOptionPane.showConfirmDialog(null, "Make another triangle?",
"Confirmation",
JOptionPane.YES_NO_OPTION);
keepPlaying = (selection == JOptionPane.YES_OPTION);
}
return keepPlaying;
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: for looping

#2 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: for looping

Posted 11 April 2007 - 05:52 PM

Alright, so in your drawTree() method, your for loop looks like this:
for(int count = 0; count < 39; count++)
{
System.out.print(" ");
}



All this is doing, is printing 39 blank spaces in the console because the only command being run through each iteration of the for loop is your System.out command. To fix this, you need to add more code in between the two brackets of the for loop.

Here's some help:
What you're looking to do is create a triangle of asterisks up to a given number. So, the first thing you need is the height, which you get when you ask for
height = scanner.nextInt();


The problem is that you don't do anything with this value.

What you're looking to do is have one line print one asterisk, the second line print two asterisks, the thrid line print three asterisks, up to whatever given height you wish. This means that your for loop must be run a number of times determined by the variable height. To do this, make your for loop look like this:
for(int x = 0; x < height; x++)
{

}



This for loop will now run as many times as determined by the variable heigth, which you ask for.
Your second task is to print an additional asterisk for each line that the console moves down. A good way to do this, would be to add another for loop inside of your original for loop:
for(int x = 0; x < height; x++)
{
  for(int y = 0; y < x + 1; y++)
  {
	System.out.print("*");
  }
}



The important part to notice is in the second for loop, the expression:
y < x + 1
This is important because x is the variable used in the first for loop and each time that the first for loop completes a cycle, the value of x will increase. So, the first time, x = 0 and the second for loop will be run only 1 time. The second time through x = 1 and the second for loop will run 2 times. And so on.

Now, the only thing left to do is move down to the next line after all of the asterisks for a given line have been printed. The best way to do this is just with a System.out.println() call. Do this after the second loop.

for(int x = 0; x < height; x++)
{
  for(int y = 0; y < x + 1; y++)
  {
	System.out.print("*");
  }
  System.out.println();
}



I hope that this helps.
Enjoy.
Was This Post Helpful? 0
  • +
  • -

#3 bigeyedfish9614  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-April 07

Re: for looping

Posted 11 April 2007 - 08:03 PM

View Postkeems21, on 11 Apr, 2007 - 05:52 PM, said:

Alright, so in your drawTree() method, your for loop looks like this:
for(int count = 0; count < 39; count++)
{
System.out.print(" ");
}



All this is doing, is printing 39 blank spaces in the console because the only command being run through each iteration of the for loop is your System.out command. To fix this, you need to add more code in between the two brackets of the for loop.

Here's some help:
What you're looking to do is create a triangle of asterisks up to a given number. So, the first thing you need is the height, which you get when you ask for
height = scanner.nextInt();


The problem is that you don't do anything with this value.

What you're looking to do is have one line print one asterisk, the second line print two asterisks, the thrid line print three asterisks, up to whatever given height you wish. This means that your for loop must be run a number of times determined by the variable height. To do this, make your for loop look like this:
for(int x = 0; x < height; x++)
{

}



This for loop will now run as many times as determined by the variable heigth, which you ask for.
Your second task is to print an additional asterisk for each line that the console moves down. A good way to do this, would be to add another for loop inside of your original for loop:
for(int x = 0; x < height; x++)
{
  for(int y = 0; y < x + 1; y++)
  {
	System.out.print("*");
  }
}



The important part to notice is in the second for loop, the expression:
y < x + 1
This is important because x is the variable used in the first for loop and each time that the first for loop completes a cycle, the value of x will increase. So, the first time, x = 0 and the second for loop will be run only 1 time. The second time through x = 1 and the second for loop will run 2 times. And so on.

Now, the only thing left to do is move down to the next line after all of the asterisks for a given line have been printed. The best way to do this is just with a System.out.println() call. Do this after the second loop.

for(int x = 0; x < height; x++)
{
  for(int y = 0; y < x + 1; y++)
  {
	System.out.print("*");
  }
  System.out.println();
}



I hope that this helps.
Enjoy.



thanks a lot man...now i have another problem. please don't think i'm just getting you guys to do the work i've just started java and i understand what needs to be done i'm just not sure how to get it done you know? I've been working on this for 5 hours straight now. Here is my new drawTree method. I have to get it so the first asterick prints 39 spaces in and then the next row prints 38 spaces in and so on. i have added the bold portion but it seems to disappear when I run the program and just prints out...any suggestions?

*
***
*****



private static void drawTree(int height)
{
for(int count = 0; count < 39; count++)
{
System.out.print(" ");
}
for(int x = 0; x < height; x++)
{
for(int y = 1; y < x * 2; y++)
{

System.out.print("*");
}
System.out.println();
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1