Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-21-2008, 05:42 PM
Member
 
Join Date: Jan 2008
Posts: 2
LinxuS is on a distinguished road
A little lost with for loops and making a design
Ask the user to enter a number between 4 and 20. If the user enters a number outside that range ask them for the number again. Based on the number the user enters, print a triangle of stars as in the following example:
If the user enters 5, your program would print the following:

Quote:
*
**
***
****
*****

(except the alignment is to the right)
so basically..
____*
___**
__***
_****
*****
without the _
So far, I've got the error checking alright, but I don't really know where to start with for the stars.

Quote:
public static void main(String [] args)
{
Scanner k = new Scanner(System.in);
System.out.println("Please enter a number between 4 and 20");
int num = k.nextInt();

while(num < 4 || num > 20)
{
System.out.println("Oops! Your number is not between 4 and 20, enter another one");
num = k.nextInt();
}
}
I'm assuming it involves a for loop, with a System.out.print(" "); System.out.print("*"); and a System.out.println("");

but any more than that I'm not sure..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-21-2008, 08:19 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,191
hardwired is on a distinguished road
Code:
int n = 5; for(int j = 0; j < n; j++) System.out.print("*"); System.out.println();
You just have to play around with print statements and loops. Imagination and play are the keys to this kind of discovery/exploration.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-21-2008, 10:01 PM
Member
 
Join Date: Jan 2008
Posts: 2
dejavujr is on a distinguished road
int n = 5;
for(int j = 0; j < n; j++)
{
for (int x = 0; x<=j ; x++) {
System.out.print("*");}

System.out.println();}

will dispaly
*
**
***
****
*****
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-21-2008, 10:39 PM
Member
 
Join Date: Jan 2008
Posts: 2
LinxuS is on a distinguished road
well I got this so far..:

for(int l = 0; l < num; l++)
{
for (int a = 0; a <= l; a++)
System.out.print(" ");

for (int x = 0; x<=l ; x++)
{
System.out.print("*");
}
System.out.println();

}

but the spaces are in reverse order that they need to be..
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-21-2008, 11:31 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Welcome to the Java Forums. I appreciate you making a valid attempt at your work.

When working out code like this, there is no better way to get this structure into your head(when you are first starting out), than to get a pencil and a piece of paper and draw out what exactly is going on. Also, choose your variable names wisely.. I changed it to lowercase i because I couldn't figure out if you were using uppercase I, lowercase L or a number one. Cheers.

Please use code tags.
Code:
int num = 5; int spaces = 5; for(int i = 0; i < num; i++) { for (int a = spaces - 1; a > 0; a--) System.out.print(" "); for (int x = 0; x <= i; x++) { System.out.print("*"); } System.out.println(); spaces--; }
See you around!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)

Last edited by CaptainMorgan : 01-21-2008 at 11:33 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-22-2008, 10:05 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
code tags
To use code tags you:
You select your code, and click the hash (#) icon when in advanced mode.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lost my javadocs orchid Eclipse 3 04-30-2008 10:45 PM
need help with program im lost lifeturn JCreator 0 02-13-2008 12:54 AM
Loops (while do etc) manupr New To Java 1 01-15-2008 04:59 AM
Absolutely Lost Lehane_9 New To Java 2 12-03-2007 07:25 PM
Help Needed - I'm so lost adlb1300 New To Java 3 11-14-2007 02:54 AM


All times are GMT +3. The time now is 10:47 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org