Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2381 | Replies: 4
![]() |
•
•
Join Date: Jul 2005
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Hi all, Im new to the site and to Java. Im having some trouble with my current assignment. I was suposed to make an isosceles triangle with asterisks based on a number entered by user. I got the first half correct but I can not figure out my loops for the second half. Do I have to start a new main to get it to descend? Here is what I have done so far. It compiles fine but will not run the descending half of pyramid. Im not sure it makes a difference but I use JCreator for IDE.
Thanks for any assistance.
import java.util.*;
public class homeWork6
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an interger from 1 to 50:");
int number = keyboard.nextInt();
System.out.println();
if (number < 1)
System.out.println("Invalid number: must be at least one.");
else if (number > 50)
System.out.println("Invalid number: cannot exceed 50.");
else // valid input print triangle of asterisks
{
// print first half
int lineCount;
int asteriskCount;
for (lineCount = 1; lineCount <= number; lineCount++)
{
for (asteriskCount = 1; asteriskCount <= lineCount;
asteriskCount ++)
{
System.out.print("*");
}//end inner for loop
System.out.println();
}//end outer for loop
for (lineCount = 1; lineCount >= number; lineCount --)
{
for (asteriskCount = number; asteriskCount >= lineCount;
asteriskCount --)
System.out.print("*");
}
System.out.println();
} //end else
}//end main
}//end class
Thanks for any assistance.
import java.util.*;
public class homeWork6
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an interger from 1 to 50:");
int number = keyboard.nextInt();
System.out.println();
if (number < 1)
System.out.println("Invalid number: must be at least one.");
else if (number > 50)
System.out.println("Invalid number: cannot exceed 50.");
else // valid input print triangle of asterisks
{
// print first half
int lineCount;
int asteriskCount;
for (lineCount = 1; lineCount <= number; lineCount++)
{
for (asteriskCount = 1; asteriskCount <= lineCount;
asteriskCount ++)
{
System.out.print("*");
}//end inner for loop
System.out.println();
}//end outer for loop
for (lineCount = 1; lineCount >= number; lineCount --)
{
for (asteriskCount = number; asteriskCount >= lineCount;
asteriskCount --)
System.out.print("*");
}
System.out.println();
} //end else
}//end main
}//end class
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
You need to come up with a formula for calculating the number of spaces to use at the beginning. There is an alternative way by using char arrays, but that is very difficult.
•
•
Join Date: Jun 2005
Location: over there... no, a bit more left than that
Posts: 56
Reputation:
Rep Power: 4
Solved Threads: 0
you could have an int which will say how many spaces to use and then while you are going through the loop and sticking down stars, you decrease the amount of spaces by one if the piramid gets fatter and then increase the spaces and decrease the amount of stars you put down when the piramid gets thinner.
make sense?
try drawing the piramid thingy on paper and then look at what it is and then also look at what changes in the next line. put little blocks down as spaces.
make sense?

try drawing the piramid thingy on paper and then look at what it is and then also look at what changes in the next line. put little blocks down as spaces.
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Rep Power: 4
Solved Threads: 0
No fancy stuff needed... your for statement is just screwed up for second half.
for (lineCount = number; lineCount >= 0; lineCount --)
{
for (asteriskCount = lineCount; asteriskCount >= 0;
asteriskCount --)
System.out.print("*");
}
System.out.println();
} //end else
You have to start at the condition you used going up and then use the start you used going up as the condition coming down. Just using -- won't cut it.
for (lineCount = number; lineCount >= 0; lineCount --)
{
for (asteriskCount = lineCount; asteriskCount >= 0;
asteriskCount --)
System.out.print("*");
}
System.out.println();
} //end else
You have to start at the condition you used going up and then use the start you used going up as the condition coming down. Just using -- won't cut it.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode