60 // This is the second break down
61
62
63 int n = 4;
64 for (int x = 1; x < n; x++) // the x(2) was incrimented by 1 from the first break down
65 // since of the x++
66 {
67 System.out.println(); // makes it on a new line
68
69 for (int y= 1; y < 2; y++) // the x gets changed to 2, now how does this work?
70 // it makes sence if 1 since that seems to get the desired result
71 // but thanks to the y-- one we had a zero when we left
72 // and since the int y = 1 was intillized already with the
73 // first time around it does not run anymore otherwise that would
74 // screw things even more up.
75 // so if it is zero that will work since zero is less then 2
76 // but that will not get use the desired result.
77
78 {
79 System.out.print(" "+ y);
80 }
81
82 for (int y = x; y > 0; y--)
83
84
85 {
86 System.out.print(" " + y);
87
88 }
89 }
Originally posted by Michael Hubele:
This was from another topic(not my topic someone elses topic) here but I decided to start a new topic since I feel for me this has branched out so much more then what the topic was about.
I understand loops(well I thought I did till this) like I can do basic loops and stuff but this is alot harder then I am use too so I am getting confussed and not sure what is going on anymore.
Here is the orginal topic: https://coderanch.com/t/401832/java/java/new-Java-programming
Here is a link to my problem I was trying to break it up into parts and writting comments of what is happening most of my comments ask questions too.
I hope what I wrote makes sence it is very hard to break these things down and then try to explain what is happening to them I also hope this link works.
http://i.1asphost.com/howser/loopy3.html
So basicly this incriments by 2 stars each time till it reaches 9 stars then stops and goes back down but the reverse way.
So I figure I will break them into 2 sections 1 going up the other going down.
[My Blog]
All roads lead to JavaRanch
Originally posted by Michael Hubele:
Originally posted by Adam Price:
<hr></blockquote>
What Satou said. But also, in case you were wondering, you messed up big in the second "for" loop. You start with y=0
You stop when y=x
You make x one bigger each time through the loop.
In other words, y never changes, and the test in the second clause of the for loop never fails.
Fixing this doesn't fix the code, though. Do what Satou said.[/QB]
[My Blog]
All roads lead to JavaRanch
Originally posted by Satou kurinosuke:
My two euros advice:
"It's working so it's alright" way of thinking is not a good way of thinking in programming. You'll understand that when you'll have to correct other people's code. You'll see all the nonsense stuff and say "What the hell is he doing ?".
Anyway, try to refactor this as soon as possible. Imagine you want to print a 100 stars diamond, do you think you'll write if() statements for each line ?
About the spaces, come on Michael, you should try to figure this out by yourself. Your biggest line is 9 stars. And you know how many stars you print per line. You can then guess how many spaces it takes to fill a line up to 9 characters. Divide this by two, and you'll get the number of spaces for each side.
Originally posted by Satou kurinosuke:
My two euros advice:
"It's working so it's alright" way of thinking is not a good way of thinking in programming. You'll understand that when you'll have to correct other people's code. You'll see all the nonsense stuff and say "What the hell is he doing ?".
Anyway, try to refactor this as soon as possible. Imagine you want to print a 100 stars diamond, do you think you'll write if() statements for each line ?
Moreover, you already understand that it is painful to output spaces. Why ? Because your code is not appropriate for this. So rewrite it first, and add the formatting stuff after.
About the spaces, come on Michael, you should try to figure this out by yourself. Your biggest line is 9 stars. And you know how many stars you print per line. You can then guess how many spaces it takes to fill a line up to 9 characters. Divide this by two, and you'll get the number of spaces for each side.
Try it, and if you still have problems, I'll be glad to help you and post the code.
This is what I have :
[ January 04, 2006: Message edited by: Satou kurinosuke ]
Originally posted by Adam Price:
<hr></blockquote>
Yeah. What he said.
FWIW, this exact problem (in two pieces) is addressed in Reges and Stuart's book
Building Java Programs If you click on the link and scroll down to section 2.5.3, you'll see that they have a nice, methodical system for solving problems of this nature. I would copy it here, but the formatting of their table would be a little tricky in UBB.
-Adam[/QB]
Originally posted by Michael Hubele:
I did not understand too much of 2.5.3 but I heard of "Pseudocode" before and maybe I should invest some time learning it? maybe it will help me break up my questions easier before I code it.
So is there any good tutorials Pseudocode? Like it seems Pseudocode makes it a bit easier to break down a problem and write it in away that it is still in english but it also is written in away so it can be easily changed into code.
Like that is my biggest problem right now it taking a problem and breaking it up into peices to start coding.
So if there are any good tips that would be nice.
By the way I finished the dimond thing useing your guy's improved method I have not figured out how to do the space yet though...
and POOF! You're gone! But look, this tiny ad is still here:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|