Results 1 to 5 of 5
  1. #1
    cmb3587 is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default While loop triangle problem

    My task is to take a user input that ranges from 1-50 and use it to create a triangle of asterisks. I.E:

    Enter number: 5

    *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *

    I know that i need 2 seperate embedded while loops and that the first one creates a row of asterisks equal to the row number until you reach the input and the second subtracts 1 asterisk until you reach 0.

    I can not figure out what is wrong with my code to do this...can someone help please.

    Java Code:
    import java.util.Scanner;
    
    public class triangle {
    
    	public static void main(String[] args) {
    		Scanner kbd = new Scanner(System.in);
    		String asterisk;
    		System.out.print("Enter a triangle size (between 1 and 50): ");
    		int number = kbd.nextInt();
    		asterisk = "*";
    		int length = asterisk.length();
    		int lines = 0;
    		while (lines <= number){
    			System.out.println(asterisk);
    			lines++;
    			while (length <= number) {
    				System.out.print(asterisk);
    				length++;
    			}
    		}
    		while (lines > 0 ) {
    			System.out.println(asterisk);
    			lines--;
    			while (length <= number) {
    				System.out.print(asterisk);
    				length++;
    			}
    		}
    
    		
    	}
    }

  2. #2
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    what is the difference of inputs from 1 - 50?

  3. #3
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    correct me if im wrong

    input: 3

    *
    **
    ***
    **
    *

    input 2

    *
    **
    *

    input 4

    *
    **
    ***
    ****
    ***
    **
    *

  4. #4
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    :) try this

    Java Code:
    public static void main(String[] args) {
    		Scanner kbd = new Scanner(System.in);
    		final String ASTRTISK = "*";
    		System.out.print("Enter a triangle size (between 1 and 50): ");
    		int number = kbd.nextInt();
    		int lines = 1;
    		final int lenght = (number * 2) - 1;
    		int loops = 1;
    		
    		while (lines <= lenght) {
    	
    			for (int index = loops; index > 0; index--) {
    				System.out.print(ASTRTISK);
    			}
    			System.out.println("");
    			
    			if (lines >= number ) {
    				loops -= 1;
    			} else {
    				loops += 1;
    			}
    			
    			lines += 1;	
    		}
    
    	}

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    azziel... I know you mean good, but it's not a good idea to give complete working code to the OPs'... too much temptation to cut & paste & forget. There's no learning process. It would be much better to give hints, links, pseudo code, explanations, exples and code snippets.

    Thanks,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM
  2. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 07:08 AM
  3. Java Zelda Triangle Problem
    By Hevonen in forum New To Java
    Replies: 3
    Last Post: 10-12-2008, 08:21 PM
  4. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 10:24 PM
  5. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •