• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not finding the right condition for a while statement to work  RSS feed

 
Andreas Eng
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

As the title says I am working with learning while-statements (also 'do' and 'for') but I am having problems adding the condition on which this will be printed.

Here is my code:


What I want this to do is to be able to print as many * as the user states he wants.


Example 1:
If the user inputs 3, the message in cmd prompt would look like this:
*
**
***

Example 2:
If the user inputs 10, the message in cmd prompt would look like this:
*
**
***
****
*****
******
*******
********
*********
**********

Appreciate any help on this matter!
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about the code, then look at the diagram. Write down a formula for how many characters you have and how many rows.
 
Andreas Eng
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Forget about the code, then look at the diagram. Write down a formula for how many characters you have and how many rows.


You must excuse my lack of ability to spot logic if I am wrong about what you mean here.
I am trying to make it dynamic instead of static. Writing it down from one to infinite would be quite a job.
It is supposed to give them as many stars as they want, but without the hassle of writing it down over and over again.
And I cannot seem to find what to put in the condition box.
 
salvin francis
Bartender
Posts: 1612
36
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Eng wrote:It is supposed to give them as many stars as they want, but without the hassle of writing it down over and over again.
And I cannot seem to find what to put in the condition box.


I think Campbell means that you should take a pencil and paper and derive a mathematical formula as to how many stars should appear.

i suggest you can start with an example like : user enters 3

Iteration 1:
Print 1 star (>> How did i calculate 1 here ?)
go to next line
Iteration 2:
Print 2 stars (>> How did i calculate 2 here ?)
go to next line
Iteration 3:
Print 3 stars (>> How did i calculate 3 here ?)
go to next line
Terminate (>> How did i decide not to go further?)

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Eng wrote:You must excuse my lack of ability to spot logic if I am wrong about what you mean here.
I am trying to make it dynamic instead of static. Writing it down from one to infinite would be quite a job.

So don't do that. That's not what Campbell suggested.
It is supposed to give them as many stars as they want, but without the hassle of writing it down over and over again.
And I cannot seem to find what to put in the condition box.

No, you haven't solved the problem. And you will never do it by coding.

Let's play a game:
In a bag I have 15 numbered pool balls. I shake the bag and take out a ball without looking at it and hand it to you. You now have to draw a triangle exactly like your computer program on a piece of paper - ie, a '*' as the first line, going down to '*****...' at the last, where the length of the last line is the same as the number on the pool ball I just handed you.

What do you do?

Now describe it to us, in English. Every single step.

When - and ONLY when - you have done that will you be able to write Java code to solve it.

HIH

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that one of the biggest hurdles everyone has to overcome when learning to program is learning to NOT write code. Programming is 90% thinking, and only about 10% typing.

Before you write a single line of code, you should feel confident you can explain to a 10yr old child what exactly needs to be done, step by step, in such a way that they know what to do in every situation. For the first several years you code, you should literally write these steps down on paper (like Winston suggests). Once you have the steps written, go over them carefully, and refine each one into more detailed steps. I often used to make 3-5 revisions, expanding my list each time, even for the most simple of programs.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:. . .
I think Campbell means that you should take a pencil and paper and derive a mathematical formula as to how many stars should appear.
. . .
That is exactly what I meant; you put it a lot better than I did.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!