473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Help with this simple triangle program

25
I am trying to figure out how to print a triangle in the shape of this depending on the user input. If they input 4 the max height would be 4.
*
**
***
****
***
**
*

I have no idea how to print the triangle after it reaches its max height. So far my code is this. Please help!
import java.util.Scanner;
Expand|Select|Wrap|Line Numbers
  1. public class Main
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         int n, line, asterik;
  6.         Scanner key = new Scanner(System.in);
  7.         System.out.println("Please enter the size of the triangle you want");
  8.         n = key.nextInt();
  9.         for(line = 1; line <= n+(n-1); line++)
  10.         {
  11.             if(line <= n)
  12.             {
  13.                 for(asterik = 1; asterik <= line; asterik++)
  14.                 {
  15.                     System.out.print("*");
  16.                 }
  17.             }
  18.             else
  19.             {
  20.  
  21.                 for(asterik = n; asterik <= line && (asterik != 0); asterik--)
  22.                 {
  23.                     System.out.print("*");
  24.                 }    
  25.             }
  26.             System.out.println(" ");
  27.         }    
  28.     }
  29. }
Feb 23 '09 #1
4 4937
JosAH
11,448 Expert 8TB
You are trying to squeeze all your logic and control flow in one big loop that handles 2*n-1 lines. You have to test whether or not you're past half way. Why not implement two simple loops, one after the other, no nesting; the first loop handles the first n lines and the second one handles the last n-1 lines.

For convenience you should define a little method that prints i characters on a line; something like this:

Expand|Select|Wrap|Line Numbers
  1. private static void printLine(int i, char c) {
  2.    for (int p= 0; p < i; p++)
  3.       System.out.print(c);
  4.    System.out.println();
  5. }
  6.  
In your two loops I described above you can simply call that little method and all those auxiliary loops that only take care of the printing are gone from your two loops.

kind regards,

Jos
Feb 23 '09 #2
milk242
25
Thanks Jos for the help that makes sense, but I was also wondering if its possible to do this task with only two for loops that are nested. I'm working out of a java book and it gives me a hint on using two for loops, one nested in another. I'm thinking its not possible because, how would you start the downward decrease of asterisk with only two loops?
Feb 23 '09 #3
JosAH
11,448 Expert 8TB
@milk242
I'd do it like this:

Expand|Select|Wrap|Line Numbers
  1. for (int i= -3; i <= 3; i++) {
  2.    int nofStars= 4-Math.abs(i);
  3.    // print 'nofStars' on a line ...
  4. }
  5.  
... but I don't find it very intuitive.

kind regards,

Jos
Feb 24 '09 #4
jkmyoung
2,057 Expert 2GB
Would have used 2 for loops like
Expand|Select|Wrap|Line Numbers
  1. for(i = 1; i <=n; i++){
  2.   //println i stars
  3. }
  4. for(i = n-1; i >0; i--){
  5.   //println i stars
  6. }
First loop increments and second loop decrements (i--).
Feb 24 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: orel | last post by:
Please, As i tried hundreds different implementation to make it work and actually didn't succeed, can someone here help me to understand and use the implementation of the Object Factory design...
4
by: orel | last post by:
<o_r_l_25@yahoo.frwrote, As the message says, to help the compiler with template parsing, I add 'typename' as you said, it now understands the expression, but but i have now those complier...
1
by: ctether | last post by:
I have never created my own program before (I'm in college right now), and I have to create a program for the length c on any triangle using sides a, b, and theta angle c. Here is what I have so far...
5
by: singhm | last post by:
Hi guys so I have a trianlge program having hard time finishing this though, I have to develop a program which is the following: Write a program that will allow the user to enter the 3 lengths...
4
by: asif929 | last post by:
I have another program to write, i will appreciate if somebody can help......prompts the user to enter positive integer, and then prints out four triangles For Example: If we enter 4 it should...
12
by: asif929 | last post by:
I am trying to write a program which creates four triangles. The program begins with prompting a user " Enter the size of triangles", number from 1 to N is the size of four triangles For Example if...
0
by: tyson verdez | last post by:
imma beginner and my iinstructur doesnt do hands on wit us we learn out of a lecture but it doesnt help some1 please help me 1. (The MyTriangle class) Write a class/program named MyTriangle. ...
5
by: Renato | last post by:
I have an array of pointers to class Shape. I create 4 items and display their values. shapes Shape *shapes; shapes = new Shape (p1); shapes = new Triangle (p1); shapes = new Polygon (p2);
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.