Results 1 to 16 of 16
- 09-26-2010, 03:35 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
Displaying a square based on user's input
Hello,
I have to write a program that displays hollow square with asterisks or plus sign or any sign. The sign is not important.
The size of the square is entered by the user. For example, when the user enters 1 the program displays
+++
+ +
+++
when enters 2 it displays something like this
++++
+ +
+ +
++++
and so on.Up until size 20. Basically it has to be a square, similar to the 8-bit figures displayed in DOS with asterisks or plus sign and with every bigger number inputted by the user it has to expand.
I coded up to size 2:
Java Code:import java.util.Scanner; public class Square { public static void main( String[] args ) { String str = "+"; int x; Scanner input = new Scanner(System.in); for(int a = 1; a <= 21; a++) { System.out.print("\nEnter size of the side: "); x = input.nextInt(); if( x==1 ) { System.out.printf("%s%s%s", str, str, str); System.out.printf("\n%s %s",str, str); System.out.printf("\n%s%s%s",str,str,str); } else if( x==2 ) { System.out.printf("%s%s%s%s",str,str,str,str); System.out.printf("\n%s %s",str,str); System.out.printf("\n%s %s",str,str); System.out.printf("\n%s%s%s%s",str,str,str,str); } and so on }
- 09-26-2010, 03:44 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 12
take a look at for loops: For Loop Tutorial
- 09-26-2010, 04:23 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
Thank you for your reply, collin389.
Last edited by luke; 09-26-2010 at 05:52 AM.
- 09-26-2010, 05:53 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
OK, using your advice I have changed the two if statements to
Java Code:if( x==1 ) { for( int i=1; i < 3; i++ ) { System.out.println("" + s +s ); } } else if( x==2 ) { for( int i=1; i < 4; i++ ) { System.out.println("" +s +s +s ); } }
So if x==2 I am getting
***
***
***
Any ideas?
- 09-26-2010, 07:38 AM #5
That's because you are adding the same string instead of a space.
Realize this:
Java Code:String x = "+"; String y = x+x+x;
If, instead, you wanted "+ +", you would do something like this...
Java Code:String x = "+"; String y = x+" "+x;
In your case, also, you should realize that you don't actually need two if statements--notice that in each for loop, your constraining variable (i < ...) is equal to x+2.Last edited by Zack; 09-26-2010 at 07:43 AM.
- 09-26-2010, 08:09 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
Thanks for your reply, Zack.
But if I do this
Java Code:String x = "+"; String y = x+" "+x;
+ +
+ +
+ +
while I need (for square size 2)
+++
+ +
+++
and for square size 3 it has to be
++++
+ +
+ +
++++
and so on. To simplify - the number of "+" signs in the sides of the square corresponds to the integer, inputted by the user.
- 09-26-2010, 08:19 AM #7
Then your for loop won't handle everything. Think of it like this:
Java Code:// x represents the width of the square, so 2 == ++ at the top. print +, x times; repeat this x-2 times: print +, then a space x-2 times, then a + print +, x times;
Hopefully that helps you think a little bit--the for loop should only handle the "repeat" portion of the code, whereas the other parts should be procedural on their own.
I'm off to bed now, so hopefully this can get you going at the very least--if not, I'm sure someone else can help you out before I'm back tomorrow. Good luck!
- 09-26-2010, 08:59 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Have a look at this little method:
Java Code:void line(char c, char m, int n) { System.out.print(c); for (int i= 0; i < n; i++) System.out.print(m); System.out.println(c); }
kind regards,
Jos
- 09-26-2010, 07:24 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
Thanks for your kind replies, Zack and JosAH. I have just got out of bed.
I'll try your advise and will be back.
- 09-27-2010, 12:22 AM #10
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
Thanks to everybody for the help. It was really valuable.
- 09-27-2010, 05:51 AM #11
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
It's about the same stupid square. I realized that actually instead of printing a square it gives me a rectangular. It freaks me out because I don't know what to do. I tried many things. You can take a look at the picture what is going on.
The code is
Java Code:public static void main( String args[] ) { Scanner inp = new Scanner(System.in); System.out.print("Enter number: "); int x = inp.nextInt(); char s = '*', e = ' '; if( x == 4 ) { for( int y=1; y < 5; y++ ) System.out.print(s); System.out.println(); for( int a=1; a < 3; a++ ) { System.out.print(s); for( int i=1; i < 3; i++ ) System.out.print(e); System.out.print(s); System.out.println(); } for( int y=1; y < 5; y++ ) System.out.print(s); } }
- 09-27-2010, 06:28 PM #12
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
Nobody knows how to make it real square?
- 09-27-2010, 06:33 PM #13
What is a real square?
What you printed had 4 sides with the same number of elements.
To get a "visually square" shape, you need to find a character that takes the same amount of space horizontally as the vertical spacing between lines printed on the console.
- 09-27-2010, 08:14 PM #14
Member
- Join Date
- Sep 2010
- Posts
- 62
- Rep Power
- 0
Thanks for your reply, Norm.
I thought I was printing an extra horizontal line.
I noticed when I press Alt+Enter in order to get the Command Prompt window maximized the square is a "square". But when the console window is minimized it creates the impression that there is an extra horizontal line.
- 07-14-2012, 07:37 AM #15
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
Re: Displaying a square based on user's input
public
If you want to make it look and feel more semetrical. Then you can just add a space behind the asterisk and add a double space to the interior.
You can do the same with the above for loop example as well.
Java Code:public static void main( String args[] ) { Scanner input = new Scanner(System.in); int side, width; System.out.print("This program prints an asterisk square!"); System.out.println(); System.out.print("Enter a side length: "); side = input.nextInt(); int hight = side; if( side <= 20 && side > 0) { while(height >= 1){ width = 1; while(width <= side){ System.out.print("* "); //There is an asterisk and a space here width++; while(height >=2 && height <= side-1 && width <= side-1){ System.out.print(" "); //There's two spaces here width++; } } --height; System.out.println(); } }else{ System.out.print("Out of bounds!"); } System.out.println(); }
Last edited by bojangle; 07-14-2012 at 07:49 AM. Reason: remove an image
- 07-14-2012, 10:43 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Displaying Multiple Tables based on Input
By Berzerk in forum New To JavaReplies: 11Last Post: 06-17-2010, 08:54 AM -
need help. problems storing and displaying multiple user input
By dDarko in forum New To JavaReplies: 3Last Post: 10-28-2009, 02:45 AM -
Reading data from csv file based on specific input
By jaiminparikh in forum Advanced JavaReplies: 14Last Post: 02-13-2009, 10:07 PM -
Perfect Square Array Input Using For Loop
By dalangley in forum New To JavaReplies: 9Last Post: 01-27-2009, 02:33 AM -
Why "Exception while reading user's input as an int"
By soc86 in forum New To JavaReplies: 1Last Post: 01-23-2009, 05:13 PM
Bookmarks