Hey guys, I'm new to programming and doing my first ever course in this field, which is, you guessed it, intro to java. I have an assignment which i'm a bit puzzled by and I'm fairly certain some of your expertise might help me out. The task I have to do is have a user enter a number of his choice, which in turn will create a triangle of "*".
So for example if they enter 4 then it would be:
It's essentially a practice on the while loop (I hope so) and also the int input by the user but my lack of practice really mentally blocks me from trying it out and I'm hoping someone can lead me in the right direction.
I would really appreciate an explanation and some bits here and there just so I can put it on eclipse and play around with it.
I think you misunderstood Bear. When he said "Then write, in English, what you need to do to accomplish this. " he means write out what you think the program needs to do, each step, on a bit of paper. By thinking through the logical building blocks of the program without thinking about programming, it will help make the solution clearer in your head.
Welcome to the Ranch Oleg.
You have to be more foolish, so that you can attempt stupid[brainy] things! So, as Bear said, pen down the English literature on how you will achieve this using some loop. Then convert that into runnable code.
I'm blocked at how to code the part system.in
User input from console is always in java.lang.String format, so you can use java.util.Scanner method nextInt() to do this.
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Joined: Oct 13, 2005
Oleg Tchernov wrote: . . .
I'm blocked at how to code the part system.in, the part which gives the user the possibility to choose the number.
. . .
In which case hard‑code the 5. If use it as i somewhere, for the time being write int i = 5; You can change it to 6 later, and you can change it to use System.in later still. Get the star printing working first.
a CRITICAL thing to learn is that when you program, you separate out the discrete elements of what your code has to do. Campbell's post hints at it, but I think this part is often not given the emphasis it requires.
For example...getting the input is a COMPLETELY SEPARATE problem from printing a triangle of stars. There are thousands of reasons why you might want to get input from a user, and getting it has nothing to do with what you're going to do with it. So, you separate out that part.
Further, printing a triangle of stars really has nothing to do with getting input from a user. I may want to print out the triangle based on data I read from a file, or pull from a database, or that is sent via a TCP/IP connection...again, the point is that IT DOESN'T MATTER WHERE the data comes from. Assuming I have a value from SOMEWHERE, I need to print out a triangle.
By separating out these components, you make the task possible. Michelangelo didn't take a block of marble and carve David by tackling the whole thing at once...he did it in teeny, tiny, incremental steps, constantly looking at what he did as he did it.
So let's assume you are just working on the triangle printing. Note that that can and should be broken down further. For example, if i were doing it, I'd probably have a method that printed X stars on a single line, where X is a parameter passed to it.
Then I would call that method over and over to print first 1 star, then 2, then 3...until i am done printing everything.
Each piece should be written independently from the others, and THOROUGHLY tested before you start the next.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors