• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Not finding the right condition for a while statement to work

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 76821
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?)

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76821
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic