Results 1 to 4 of 4
  1. #1
    confused2000 is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default 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 01:41 AM.

  2. #2
    confused2000 is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default

    damn the square doesnt show correctly for 7. the second column should be on the right

  3. #3
    confused2000 is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default

    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 &#37; 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();
    }






    }
    }

  4. #4
    unhurt is offline Member
    Join Date
    Oct 2007
    Posts
    21
    Rep Power
    0

    Default

    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


    Java 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 08:24 AM.

Similar Threads

  1. Java assignment
    By xtianah77 in forum New To Java
    Replies: 1
    Last Post: 02-17-2008, 11:54 PM
  2. Taking Java In School Need Help
    By xEuPhOrIcSx in forum New To Java
    Replies: 7
    Last Post: 02-04-2008, 08:02 AM
  3. Replies: 2
    Last Post: 01-07-2008, 07:45 PM
  4. Beginner Needs Help w/ Program for School
    By badness in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:51 PM
  5. for Assignment plz help
    By assamhammad in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 08:35 PM

Posting Permissions

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