Hi new to java trying to do something new :) any help would be appreciated!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Hi new to java trying to do something new :) any help would be appreciated!

  1. #1
    Join Date
    Nov 2011
    Posts
    1

    Hi new to java trying to do something new :) any help would be appreciated!

    hi, I am trying to complete the following task (purely for fun) I would really appreciate it if someone could adapt my code:

    Write an a Java application which prints a diamond in a
    square grid of dots whose side length is input to the
    application. The side of the square must be an odd
    number. If the input data is invalid an error message is
    output. For example, if the input data was 10 then the
    error message
    Size (10) invalid must be odd
    would be printed. If the input data was -5 then the error
    message
    Sides of square must be positive
    would be printed

    Here is my code i have got:
    public class Stars
    {
    public static void main(String[] args)
    {
    System.out.println("Enter size of Diamond");

    System.out.println();
    int longestRow = BIO.getInt();

    for(int row=1 ; row<=longestRow ; ++row)
    {
    int i = (2*row) - 1;
    if(i>longestRow) i = 2*(longestRow-row+1) - 1;
    for(int j=0 ; j<(longestRow-i)/2 ; ++j) System.out.print(".");
    for(int j=0 ; j<i ; ++j) System.out.print("*");
    for(int j=0 ; j<(longestRow-i)/2 ; ++j) System.out.print(".");
    System.out.println();
    }
    }
    }

    this looks like this when ran:
    5
    ..*..
    .***.
    *****
    .***.
    ..*..

    I am trying to make it look like this:
    ..*..
    .*.*.
    *...*
    .*.*.
    ..*..

    Thanks Gareth

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Hi new to java trying to do something new :) any help would be appreciated!

    hi, I am trying to complete the following task (purely for fun)
    That's a coincidence because we get homework questions based on this type of question every year around November time.

    I would really appreciate it if someone could adapt my code:
    By adapt your code do you mean fix it? We generally don't do homework (sorry, fun tasks ) for people but we will give advice on how you can solve it yourself.

    When posting code please use code tags.

    You've got a solid diamond printing out so how do you make it hollow? Well the code that prints the asterisk(s) needs to print just the first and last one of the line so identify which for loop prints the asterisks and add an if-else statement to print an asterisk in certain conditions and a dot in others.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2011
    Posts
    189

    Re: Hi new to java trying to do something new :) any help would be appreciated!

    you people learning in schools are weird for drawing diamonds. Just a tip: The program is almost the same as your first, but the outter diamond has an inner diamond made out of dots. Since the inner diamond must be made out of odd numbers as well it suddenlly gets easier

  4. #4
    Join Date
    Nov 2011
    Posts
    189

    Re: Hi new to java trying to do something new :) any help would be appreciated!

    i dont quite get the utility of this line. It could be made much easyer, with 3-4 more commands


    i = 2*(longestRow-row+1) - 1;

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center