This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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!!!
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 ]
Joined: Sep 30, 2004
The message board poseted the output wrong. the correct output should look like this (ignore the x's):
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.
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
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
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 ]
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 ]