• Post Reply Bookmark Topic Watch Topic
  • New Topic

Some help with a problem  RSS feed

 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone show me how to do something like this? or post hints

Write a program that uses a loop to print out a triangular pattern like this:
*
**
***
****
*****
The program should use a constant that defines the symbol used, in the example above �*�, and another variable that determines the number of rows of the triangle. Run your program several times so that it prints triangles of different sizes.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

You'll find we don't directly answer questions like this in the beginner forum, but try to lead you to discover something new on your own.

Your program is going to print some number of rows. Let's figure there will be a loop that executes for each row. And it's going to print some number of stars in each row. Sounds like another loop for stars inside each row.

Can you express the number of stars as some relation to the row number? First row: one. Second row: two ... it's coming to me.

Now start some code and let us see what you get! Are you comfortable writing a main() method? Doing a for loop? Let us know where you are and where you're stuck and we'll go from there!
[ October 17, 2005: Message edited by: Stan James ]
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes im comfortable writing that, the problem is how i get my program to output my results. Ill be working on it again later at uni.

Thanks for reply
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code is looking really noobish atm but its 3am and it was doing my head in.


import java.util.Scanner;

class triangle {


public static void main (String[] args){


// Scanner declarations and loop

int rows = 0;
final char result = '*';

System.out.print("Enter a number to square: ");
Scanner in = new Scanner(System.in);
rows = in.nextInt();


while (rows > 0) {

rows = (rows * rows);
System.out.println (result);

}


}

}


My output is like, if i put the rows as 5 it will output

*
*
*
*

getting there i suppose.
[ October 17, 2005: Message edited by: ste fing ]
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.Scanner;

class triangle {


public static void main (String[] args){


// Scanner declarations and loop

char result = '*';
int rows = 0;

System.out.print("Enter a number: ");
Scanner in = new Scanner(System.in);
rows = in.nextInt();

// For Loop

for (int r = 0; r < rows; r++) {
System.out.println (result);
}


}

}


argh
[ October 17, 2005: Message edited by: ste fing ]
 
Wayan Saryada
Ranch Hand
Posts: 119
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You can use loop twice, one for iterating the rows and the other for writing the "*". So you might have a loop like below.

 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it working thanks for the tips, heres how i got it.


import java.util.Scanner;

class triangle {


public static void main (String[] args){


// Scanner declarations and loop

final char result = '*';
int rows = 0;

System.out.print("Enter a number: ");
Scanner in = new Scanner(System.in);
rows = in.nextInt();

// For Loop

for (int r = 0; r <= rows; r++) {

for (int l = 0; l < r; l++)
System.out.print('*' );
System.out.println();


}

}

}
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by I Wayan Saryada:
Hi,

You can use loop twice, one for iterating the rows and the other for writing the "*". So you might have a loop like below.




yea i done that. I had it all printing out the stars, but i needed.

System.out.println();

I didnt see your message till after i postd my finished code. Otherwise i wouldnt have spent 30mins trying to find out i needed println

Thanks for help.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, those 30 minutes shouldn't be a complete waste. Hopefully you learned something. More importantly, you will remember this when it comes up again.

Layne
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!