Results 1 to 6 of 6
  1. #1
    PPhillips is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Only Loops Program

    Using a loop and only loops, design a program that creates the following ASCII art designs when the user enters in 9:
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    The rows of asterisks should line up on the left side and so on as i tried to show above.
    Here is the code i have so far. please help out and give suggestions

    import java.util.Scanner;


    public class OnlyLoop {
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int star, int1, zero = 0;
    String space;

    while (zero < 1){
    System.out.println("Enter number: ");
    int1 = scan.nextInt();
    space = " ";

    }}}

    space represents the number of spaces and star represents the number of asterisks. thanks for your cooperation

  2. #2
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Only Loops Program

    Well.. I would use a double for loop. The first for loop is for the value of asterisks the user enters (in your example, you use 9). The second for loop is for actually printing out the asterisks in order of lines (first line prints 1 asterisk, second line prints 2 asterisks, etc). You would only need one integer value for this problem, and that should be the value the user enters.

    Also, a tip for next time: When you want to present your code, you should put them in [CODE][/CODE] tags, for neatness' sake.
    Last edited by CuppaCoffee; 05-08-2013 at 09:39 PM.

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Only Loops Program

    I'll give you a simple idea how you should use two for loops, but writing full program is up to you:

    Java Code:
    import java.util.Scanner;
    
    public class OnlyLoop1 {
    
    	public static void main(String[] args) {
    		System.out.println("Enter a number between 1-20:");
    		Scanner scan = new Scanner(System.in);
    		int number;
    		
    		number = scan.nextInt();
    		
    		for(int i = 0; i < number; i++) {
    			for(int j = 0; j < number; j++)
    				System.out.print("*");
    		
    		System.out.println();
    		}
    	}
    
    }

  4. #4
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Only Loops Program

    Quote Originally Posted by cselic View Post
    I'll give you a simple idea how you should use two for loops, but writing full program is up to you:

    Java Code:
    import java.util.Scanner;
    
    public class OnlyLoop1 {
    
    	public static void main(String[] args) {
    		System.out.println("Enter a number between 1-20:");
    		Scanner scan = new Scanner(System.in);
    		int number;
    		
    		number = scan.nextInt();
    		
    		for(int i = 0; i < number; i++) {
    			for(int j = 0; j < number; j++)
    				System.out.print("*");
    		
    		System.out.println();
    		}
    	}
    
    }
    That code is actually incorrect.. specifically line 13. The second for loop's condition says that as long as the value j is lower than 'number', it will continue printing out asterisks. If the number I pick is 9, it will print out 9 asterisks, nine times.

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Only Loops Program

    ^Offcourse that my code is incorrect, and that's because I want to give an idea to OP, and not to write all code for him.

  6. #6
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Only Loops Program

    You've basically given him the whole program though.. I don't see how he can learn to solve a problem by having him correct an error, but to each their own, I guess.

Similar Threads

  1. Help with Java program (Loops)
    By phoenixblue in forum New To Java
    Replies: 1
    Last Post: 11-13-2012, 12:48 AM
  2. How do I scroll using loops in a program?
    By joes in forum New To Java
    Replies: 1
    Last Post: 02-18-2011, 01:11 PM
  3. HELP - Creating OPTIONS in program and LOOPS
    By whateverme in forum New To Java
    Replies: 14
    Last Post: 12-07-2010, 06:55 AM
  4. Help combining loops into 1 program.
    By kewlgeye in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 10:58 AM

Posting Permissions

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