• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

for loops

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all need to find out how to produce this using for loops
*
**
***
****
*****
I can't quite get my head round this and need to get this to work for college. I will then need to do another application which goes the othre way
*****
****
***
**
*
If anyone would like to help this poor lad and put him out of his misery I would be grateful
TIA
ken
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this is a homework assignment, doubt anyone would put up to code to solve the problem, but probably could get some hints
You obviously can tell you need a for loop. You're printing out a String of *'s, that each time increases in size by one. Think about what operators you could use on a String, to continually add to it, and then print it out.
For the second part, since it is the opposite of the first, you're going to do the opposite operation.
That should get you going hopefully!
Jason
 
ken dodd
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cheers jason here's what I have at the mopublic class triangle
{
public static void main (String args [])
{
for (int i = 0; i < 5; ++i);
{
for (int a = 0 ; a < 5; ++a);
System.out.println("*");
}
}
}
I get it to print 1 * out but thats it haven't done much on operators yet but will try to figure it out
cheers
ken
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, ken dodd
These two logic are perfect for the product you have asked ��.
Try it out�
----------------------------------------------------

------------------

[This message has been edited by Ikram Soomro (edited October 20, 2001).]
[This message has been edited by Cindy Glass (edited October 23, 2001).]
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or someone could just give you the answers like that and ruin the whole learning process
Anyway, there are a couple of ways that I can think of to solve this problem, off the top of my head. The one is above, the other is using just one for loop, but adding a * each iteration to a String, and then printing the String. Both are equally valid solutions.
One of the main things to remember when doing formatting like this, or in general, is the use of System.out.print() and System.out.println(). One prints out on the same line, the other prints to the current line and then adds a return character to the end, making the next print out to be on a new line.
Jason
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol....you're funny Jason ....I agree with Jason's method, using one for loop and adding an additional * to the String each time through. If you're trying to learn for loops, you'll just confuse yourself with a for loop inside another for loop.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kenn,
more hint
watch ur semicolone
 
ken dodd
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cheers for the replys
I did figure out how to get the loops to print

but I wrote the second program the wrong way round and needed to produce this
//***** Please ignore the ///
///****
////***
/////**
//////*
I have changed the above prog as this

and end up with this
*****
****
***
**
*
I have tried changing the values and making the >= change but just end up with never ending loops. I just need to be pointed in the right direction as 5 hrs to working this out is doing my head in.
cheers
ken
[This message has been edited by ken dodd (edited October 21, 2001).]
[This message has been edited by Cindy Glass (edited October 23, 2001).]
 
Ikram Soomro
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ken dodd,
If you say i'll tell u that code, but in this manner you'll fell dificulty to learn Java....
I'm giving u hint for the problem...
i think your question is
to print
like
/////*
////**
///***
/*****
then try nested loops after each other
like...
{
for()
{
for()
{
for()
}
}
}
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic