Forums Register Login

For loop Pyramid Program

+Pie Number of slices to send: Send
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!!!
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
NOTE: The readInt method is probably something like this...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send


Output:

1
232
34543
4567654
567898765
67891011109876
78910111213121110987
89101112131415141312111098
91011121314151617161514131211109
10111213141516171819181716151413121110
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
Hi Seetharaman. Since I am new, I wasn't aware of this. I will follow the rules now onwards. Thanks for the information.
+Pie Number of slices to send: Send
 

Shishir Rmv wrote:Thanks for the information.


you are welcome
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Welcome to the Ranch

We do not usually like having complete answers, but after 9 years it is unlikely to do any harm
+Pie Number of slices to send: Send
Thanks Ritchi

that was just to finish it and mark presence
Let's go to the waterfront with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 8243 times.
Similar Threads
Recursion Formula
Making a mouse click in Java?
Question about restart
stars
For loop....Pyramid Program
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:33:44.