Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-06-2007, 03:32 AM
Member
 
Join Date: Nov 2007
Posts: 3
confused2000 is on a distinguished road
Please help... assignment for school
Write a Java program that prompts the user to enter the size of a square from 1 to 20 (inclusive) and then displays a hollow square of that size made of the character uppercase ‘X’. If the user inputs an invalid number, the program should prompt them repeatedly until their input is in the allowable range.

Examples of the output:

Enter the square size from 1 – 20: 7
XXXXXXX
X______X
X______X
X______X
X______X
X______X
XXXXXXX

Enter the square size from 1 – 20: 1
X

Enter the square size from 1 – 20: 2
XX
XX

So far i got this:

import java.util.Scanner;

public class Square1 {
public static void main(String[] args) {

Scanner keyInput = new Scanner(System.in);


int lo = 1;
int hi = 20;
int squareSize;


do
{

System.out.print("Enter the square size from 1 - 20: ");
squareSize = keyInput.nextInt();
}
while (squareSize >= lo ^ squareSize <= hi);


}
}

Last edited by confused2000 : 11-06-2007 at 03:41 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-06-2007, 03:34 AM
Member
 
Join Date: Nov 2007
Posts: 3
confused2000 is on a distinguished road
damn the square doesnt show correctly for 7. the second column should be on the right
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-06-2007, 04:29 AM
Member
 
Join Date: Nov 2007
Posts: 3
confused2000 is on a distinguished road
got this so far, but the square is not hollow, HELP!!
__________________________________________________ ______________

import java.util.Scanner;

public class HollowSquare2 {
public static void main(String[] args) {

Scanner keyInput = new Scanner(System.in);


int lo = 1;
int hi = 20;
int squareSize;

int rowCounter = 0;
int columnCounter = 0;

do
{

System.out.print("Enter the square size from 1 - 20: ");
squareSize = keyInput.nextInt();
}
while (squareSize >= lo ^ squareSize <= hi);

while (rowCounter < squareSize)
{
// Loop produce columns
while (columnCounter < squareSize)
{
if (rowCounter % 2 == 0)
{
System.out.print("X");
}
else
System.out.print("X");
columnCounter++;
}

// Increment and reset for the next row
rowCounter++;
columnCounter = 0;
System.out.println();
}






}
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-12-2007, 10:12 AM
Member
 
Join Date: Oct 2007
Posts: 21
unhurt is on a distinguished road
erm, well i don't know about ur while loop is it necessary for ur assignment but i change it to for loop it works fine.... hope u don't mind the while and for though.... but this is what i got just put this as ur while loop


Code:
for (int i=0; i<squareSize; i++) { System.out.print("X"); } for (int i=1; i<squareSize-1; i++) { System.out.print("\nX"); for (int j=1; j<squareSize-1; j++) { System.out.print("-"); } System.out.print("X"); } if (squareSize != 1) { System.out.println(); for (int i=0; i<squareSize; i++) { System.out.print("X"); } }

Last edited by unhurt : 11-12-2007 at 10:24 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java assignment xtianah77 New To Java 1 02-18-2008 01:54 AM
Taking Java In School Need Help xEuPhOrIcSx New To Java 7 02-04-2008 10:02 AM
Need help with a program for high school regarding multi-dimensional arrays. Torque New To Java 2 01-07-2008 09:45 PM
Beginner Needs Help w/ Program for School badness New To Java 2 11-24-2007 09:51 PM
for Assignment plz help assamhammad New To Java 1 11-06-2007 10:35 PM


All times are GMT +3. The time now is 10:57 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org