Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: need help with a mumbers triangle

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post need help with a mumbers triangle

    Hi everyone,
    I'm assigned to make a triangle using numbers and I'm only allowed to use while loop
    the program should give output like this
    *****1
    ****121
    ***12221
    **1222221
    *122222221
    (Ignore the "asterisks,*")
    I've sorted out how to make a triangle but I can't figure out how to display the above written output.
    this is the code
    class numtriangle{
    public static void main(String a[]){

    int trianglesize =10;
    int num = 1;
    int spaces = trianglesize / 2;
    int count = 0;
    while (num <= trianglesize)
    {

    while (count < spaces)
    {
    System.out.print(" ");
    count++;
    }
    count = 0;
    while (count < num)
    {
    System.out.print("1");
    count++;
    }
    count = 0;
    spaces--;
    num++;
    num++;
    System.out.println();
    }
    }
    }

    and this generates the below output
    *****1
    ****111
    ***11111
    **1111111
    *111111111

    any of you guys might want to help would be highly appreciated


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help with a mumbers triangle

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Look at what is printed on each line and work out a design for the code that will follow the rules to generate that output.
    How many *s are on each line?
    How many digits are on each line?
    The first and last digit must be a 1, the rest are 2

    Work out the logic by making a list of the steps the code should do.
    When you have the logic as a list of steps, then write the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with a mumbers triangle

    Sorry for not wrapping my code, I'm new to this forum so don't know how to do the posting, I'll be careful next time,
    I don't want to print stars, just numbers, I've been trying to sort it out for at least 4 hours but can't figure it out I'm not asking you to write the code for me, just the part with 1s and 2s.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help with a mumbers triangle

    I assumed the *s were supposed to be spaces.
    You need to work out the logic for what is printed on each line.
    How many digits are printed on the first line? How many on the second?
    When there are more than 1 digit, the first and last are 1s the inside digits are 2s
    How many inside digits are there on each line?

    Make a spread sheet with the line number in column1, the number of digits in column2 and the number of inside digits in column3. Then work on the formulas needed to compute the contents of columns 2 & 3 based on what is in column 1. When you have those formulas, you should be able to think about the code that will do it.

    If you have problems with the formulas, post the contents of the spread sheet (3 columns) and we'll help you with the formulas.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with a mumbers triangle

    Actually I'm only studying java for three weeks, this is an assignment while I'm on the section of loops, now I can't work out or think anything because I'm trying to do it for few hours, just help me this once and I'll try to improve next time, you are right with the stars, * is space

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help with a mumbers triangle

    Think about the question I asked: Create a spread sheet with three columns as I described.
    Then work out the formulas for columns 2 & 3 using the value in column 1
    I'm only studying java for three weeks,
    There is no java programming involved in creating the spread sheet. The java coding part will come after the logic is worked out. You need to work through problems step by step. Logic first, then coding.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with a mumbers triangle

    yeah, thanks anyways.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help with a mumbers triangle

    That's how programming works: Work out the logic then write the code.
    If you can't work out the logic, you can not write a program.

    Also posted at http://www.java-forums.org/new-java/...-triangle.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. help me draw a triangle....
    By beandip408 in forum Object Oriented Programming
    Replies: 10
    Last Post: October 28th, 2010, 05:49 PM
  2. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM
  3. ASCII Triangle
    By physics in forum Loops & Control Statements
    Replies: 1
    Last Post: March 27th, 2010, 06:39 AM
  4. Triangle issues
    By FrEaK in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2010, 08:49 AM
  5. Triangle Question
    By Leeds_Champion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2009, 11:33 PM