Results 1 to 2 of 2
Thread: Pattern Program Help me please
- 11-22-2011, 05:36 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
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:
but I have it like thisJava Code:* *** ***** ***** *** *
NOT WANTED:
as you can see I am missing one row, how can I fix this please thank you, my code is here.Java Code:* *** ***** *** *
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(); } } }
- 11-22-2011, 05:57 AM #2
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
-
strategy pattern and bridge pattern
By jomypgeorge in forum New To JavaReplies: 2Last Post: 12-13-2010, 05:13 AM -
Program that displays JAVA word pattern.
By SNFA in forum New To JavaReplies: 10Last Post: 10-08-2010, 10:55 PM -
Class pattern to generate following pattern:-
By vxs in forum New To JavaReplies: 5Last Post: 07-14-2010, 11:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks