View Single Post
  #1 (permalink)  
Old 01-21-2008, 06:42 PM
LinxuS LinxuS is offline
Member
 
Join Date: Jan 2008
Posts: 6
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..
Reply With Quote
Sponsored Links