Results 1 to 2 of 2
  1. #1
    Kamus is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Question Pattern Program Help me please

    Write a program that displays diamonds as text using a character provided by the user. Besides specifying a character, the user will specify one of three possible heights for each diamond - 6, 12, or 22 - by providing one of the strings "short", "average", or "tall".
    Write the following methods in the program:
    Public static int checkSize(String size)
    Public static void displayPattern (int size, char ch)
    Use a do-while loop in the main method to verify that the diamond size that users enters must be "short", "average", or "tall". If invalid string is input, the program should ask for new input until the valid input is given.
    So far this is what I have compiled, but I'm still pretty stuck on what I should do.
    So far in my code I need the do-while loop, ive trying to put it but I cant make it work. The pattern also should be like this when is short ( of height six as mentioned before):

    WANTED:
    Java Code:
      *
     ***
    *****
    *****
     ***
      *
    but I have it like this

    NOT WANTED:
    Java Code:
      *
     ***
    *****
     ***
      *
    as you can see I am missing one row, how can I fix this please thank you, my code is here.

    Java Code:
    import java.util.Scanner;
    
    public class Pattern
    {
    public static void main(String [] args)
    {
    
    Scanner kb = new Scanner(System.in);
    
    String input;
    String imput;
    int size;
    char character;
    System.out.print("enter diamond size (\"short\", \"average\", or \"tall\"):");
    input = kb.nextLine().toLowerCase();    
    size = checkSize(input);
    System.out.print("Enter pattern character: ");
    imput=kb.nextLine();
    character=imput.charAt(0);
    displayPattern(size,character);
    System.exit(0);
    }
    
    public static int checkSize(String size)
    {
    if(size.equals("short"))
    return 6;
    else if(size.equals("average"))
    return 12;
    else if(size.equals("tall"))
    return 22;
    else
    return -1;
    }
    
    public static void displayPattern(int size, char ch)
    {
    
    for(int i = size; i >= 1; i-- )
    {
    int j;
    for( j = 1; j < i; j++ )
    {
    System.out.print(" ");
    }
    
    for(int k=size; k >= j*2; k-- )
    {
    System.out.print(ch);
    }
    
    System.out.println();
    }
    
    
    
    for(int i = 2; i <= size; i++ )
    {
    System.out.print(" ");
    int j;
    for( j = 2; j < i; j++ )
    {
    System.out.print(" ");
    }
    
    for( int k = size; k >= j * 2; k-- )
    {
    System.out.print(ch);
    }
    
    
    System.out.println();
    }
    
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Pattern Program Help me please

    My suggestion is to rethink your design. Your top loop block loops from size down to 1 while the bottom loop block goes from 2 upto size. What I would do is have each loop block print 3 lines (size / 2). From there work out how many spaces and how many chars are printed on each line and if they increase or decrease.

Similar Threads

  1. strategy pattern and bridge pattern
    By jomypgeorge in forum New To Java
    Replies: 2
    Last Post: 12-13-2010, 05:13 AM
  2. Program that displays JAVA word pattern.
    By SNFA in forum New To Java
    Replies: 10
    Last Post: 10-08-2010, 10:55 PM
  3. Class pattern to generate following pattern:-
    By vxs in forum New To Java
    Replies: 5
    Last Post: 07-14-2010, 11:15 PM

Tags for this Thread

Posting Permissions

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