File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes For loop Pyramid Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "For loop Pyramid Program" Watch "For loop Pyramid Program" New topic
Author

For loop Pyramid Program

David Pedisich
Greenhorn

Joined: Sep 30, 2004
Posts: 6
I'm taking Computer Science AP and I'm having a problem writting the following program. I'm farely new to java....still trying to think like a computer. Anyways




thats what the output should look like.
the number on top is what is read by the computer and increased by 2 everytime hence 3,5,7,9
the numbers printed out to the right and left are decressed by 1
my teacher says i'm suppose to use the for loop to create the program. i'm really lost. please help!!!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Let's see if I can get you going in the right direction.

"my teacher says i'm supposed to use the for loop..."

Okay. A for loop basically has three parts:

for(assignment; boolean; expression){}

And it looks like you already have your three parts identified: "...the number on top is what is read by the computer and increased by 2 everytime hence 3,5,7,9..."

You could assign "the number on top" as a starting point. Write an expression to increase by 2 every time. And use a boolean to tell you when to stop.

As for what goes inside the loop body (inside the braces): "...the numbers printed out to the right and left are decreased by 1."

So all the parts are here. Let's see what code you've written so far, and we'll go from there.



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
According to the program, you can notice that the center of the triangle,
so the condition of program for printing out the number will be
1 //i=1, the times to print 1(n)
232 //i=2, the times to print 3, n=(n+2)
34543 //i=3, the times to print 5, n=(n+2)
4567654 //i=4, .....
567898765 //i=5, ......

The program is not complete, I want to show out the steps and condition that are required to do.
Hope you can learn somethings here &
hope this help

[ September 30, 2004: Message edited by: siu chung man ]

Francis Siu
SCJP, MCDBA
David Pedisich
Greenhorn

Joined: Sep 30, 2004
Posts: 6
The message board poseted the output wrong. the correct output should look like this (ignore the x's):

xxxx1xxxx
xxx232xxx
xx34543xx
x4567654x
567898765

this is what i've written so far. i know the program won't work and the loops don't make any sense, but i'm lost in the dark on this one.



the LearningIO is are temp. reading program, we haven't gotten that far yet, accoriding to the book/teacher.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

That's an awful lot of code to write and then discover "it doesn't work". Can you try writing a smaller chunk at a time, getting it to work, and THEN adding the next part?

perhaps you can start by getting the input and printing it out - that's it.

I'm not sure what you're doing with n, btw. you get it from the user, but then you re-assign it's value in a for-loop...

once you have the input working, write a for loop the just does what you're supposed to do with the middle... so you'd want to get

1
3
5
7
9

for example.

Bascially, start smaller, do less at a time, and get it to work. Some of the sherriffs here (Dirk or Ernest - can't remember whom) will tell you to write ONE LINE OF CODE, and compile it - that way, if you get a compile error, you know EXACTLY where the problem is. Then, make sure that line does what you think it should do before writing any more.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

David: Thanks for posting your code! I didn't realize from your initial post that you were this far along.

A few things...

In your outermost "for" loop, I think you want <= in your boolean expression. Currently, none of these loops are executing because you have >=.

A char can be converted to an int. So when you say int m = y * space (where space is a char), you simply get an int value. You do not get the char displayed y times. Actually, you're already looping through y times, so all you need to do inside the loop is print a single space.

Also in this loop, y is hardcoded to always start at 4. I think you want this to change depending on the value of "row."

If you want something to happen only once (like printing your "main number"), then all you need is a statement -- not a loop. (Besides, you're setting n to zero and then testing to see if n is greater than zero, so this loop won't execute at all. And even if it did, do you really want to print zero?)

I see that n is supposed to be input by the user. Is n really supposed to be variable, or do we "know" the user is always going to input 9? (Of course, if we go beyond 9, then we'll mix in 2-digit numbers, and the pyramid won't be symmetrical without some added effort.)
[ October 01, 2004: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

NOTE: The readInt method is probably something like this...
David Pedisich
Greenhorn

Joined: Sep 30, 2004
Posts: 6
I restarted writting the program and this is what i've got. I'm using NetBeans IDE 3.6.....


import java.text.*;
import learning.*;

class Pyramid{
public static void main(String args[]){
int n;

System.out.println("Enter Number");
n= LearningIO.readInt();

for(int row=1;row>=5;row++){
for(int x=n;x>=9;x++){
System.out.print(x);
}//end of n
System.out.println();
}//end of for

}//end public
}//end class

my new problem is this part doesn't work, although it seems very basic and it should.
The user is always going to enter 1, its just for show.
I don't understand why >= presents a problem either.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
It contains problem as row never bigger than 5

when will the for loop work? The following one


If x bigger than 9 then, for loop continues to work

So, do you know what's wrong with it?

Hope this help

[ October 03, 2004: Message edited by: siu chung man ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A "for" loop basically has three parts...

for(assignment; boolean; expression){}

The "assignment" statement is executed only once, at the very beginning. The "boolean" is evaluated at the beginning of each iteration (the "top" of the loop). If true, then the body of the loop executes. If false, then the body of the loop does not execute. The "expression" executes at the end of each iteration (the "bottom" of the loop), typically to increment some value.

So, when you say "for(row = 1; row>=5; row++)..." you are assigning row = 1; but then saying to execute the loop body only if row is greater than or equal to 5. Because row is not greater than or equal to five, the body of the loop will not execute.

In other words, the boolean should express a "go" condition -- not a "stop" condition.

Currently, your code compiles but does not generate any output because neither of the loops execute. However, if you simply change your ">=" to "<=" in both loops, then you'll get output of...

123456789
123456789
123456789
123456789
123456789

That is, five rows (the outer loop) of counting from n to 9 (the nested loop).



As fred suggested above, "start smaller, do less at a time, and get it to work." So as a next step, I suggest modifying this code to work from n=1 to 9 in increments of 2 (so you get your 1, 3, 5, 7, 9), and counting down from these numbers. So your output would look like the following...

1
321
54321
7654321
987654321

This would be a good step forward in this assignment. Actually, the code you posted on October 1 is a reasonable "sketch" of the program, but you need to take care of the details first -- one step at a time.
[ October 03, 2004: Message edited by: marc weber ]
Shishir R Verma
Greenhorn

Joined: May 01, 2012
Posts: 6



Output:

1
232
34543
4567654
567898765
67891011109876
78910111213121110987
89101112131415141312111098
91011121314151617161514131211109
10111213141516171819181716151413121110
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi Shishir,
It is very old thread. the OP's last post here is dec/17/2004. he might be left javaranch. also please read DontBeACodeMill

and Welcome to JavaRanch!
Shishir R Verma
Greenhorn

Joined: May 01, 2012
Posts: 6

Hi Seetharaman. Since I am new, I wasn't aware of this. I will follow the rules now onwards. Thanks for the information.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Shishir Rmv wrote:Thanks for the information.

you are welcome
Sumit Tanwar
Greenhorn

Joined: Sep 18, 2013
Posts: 3
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Welcome to the Ranch

We do not usually like having complete answers, but after 9 years it is unlikely to do any harm
Sumit Tanwar
Greenhorn

Joined: Sep 18, 2013
Posts: 3
Thanks Ritchi

that was just to finish it and mark presence
 
Don't get me started about those stupid light bulbs.
 
subject: For loop Pyramid Program
 
Similar Threads
For loop....Pyramid Program
stars
Recursion Formula
Making a mouse click in Java?
Question about restart