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

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.

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.

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.

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

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

Quote:

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.

Re: need help with a mumbers triangle

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