Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: homework troubles, any help would be appreciated

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default homework troubles, any help would be appreciated

    Homework question: Write a program that prompts the user to enter the diameter of the diamond and the character to use for drawing the diamond and then draws the diamond.

    At the moment I just care about figuring out the diameter part.

    Here is what i have so far:

            /*
            Scanner scan = new Scanner(System.in); 
     
            int diameter = 0;
     
            System.out.print("Enter the diameter of diamond: ");
            diameter = scan.nextInt();
            */
     
            for(int i = 1; i < 6; i++)
            {
                for(int j = 1; j < 6 - i; j++)
                    System.out.print(" ");
                for(int j = 1; j < 2 * i; j++)
                    System.out.print("*");
                System.out.println();
            }
     
            for(int i = 1; i < 5; i++)
            {
                for(int j = 5; j > 5 - i; j--)
                    System.out.print(" ");
                for(int j = 7; j >= i * 2 - 1; j--)
                    System.out.print("*");
                System.out.println();
            }

    I figured if i did this first i could just replace some numbers with a variable and it would work but nothing I've tried has worked.
    I just want to know if I'm heading in the right direction. If not can someone please push me in the right direction.


    Thank you,

    Steve


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: homework troubles, any help would be appreciated

    What is your current problem? Does the code compile? If not, post the error messages.
    Does the code execute without errors? If not post the error messages.
    Post what the code does output and show what is wrong with that output and show what you want the output to be.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: homework troubles, any help would be appreciated

    Two things, you have hardcoded it to run only one specific diameter. Do you want to be able to run any diameter? What kinds of problems are you getting when you try to run it?
    And second, adding comments to your code makes it many times easier to help you with your code.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: homework troubles, any help would be appreciated

    Thank you guys for responding but I sat down with a my pen and a sheet of paper and did the loop step by step and figured it out and yes I need to add comments. Its something I need to work on.

    here is the code if anyone is interested on how it works..

     
     
     
     
    package project.pkg5part3;
     
    import java.util.Scanner;
     
    public class Project5Part3 
    {
     
        public static void main(String[] args) 
        {
            Scanner scan = new Scanner(System.in); // new object of scanner 
     
            //variables
            int diameterOfDiamond;
            String diamondCharacter;
     
            boolean flag = true;
     
     
            while(flag)
            {
                System.out.print("Enter the diameter of diamond: ");
                diameterOfDiamond = scan.nextInt();
     
                System.out.print("\nEnter the character to use for the drawing: ");
                diamondCharacter = scan.next();
     
                for(int i = 1; i <= (Math.ceil(diameterOfDiamond / 2.0)); i++)
                {
                    for(int j = 1; j < (Math.ceil(diameterOfDiamond / 2.0) + 1) - i; j++)
                        System.out.print(" ");
                    for(int j = 1; j < 2 * i; j++)
                        System.out.print(diamondCharacter);
                    System.out.println();
     
                }
                for(int i = 1; i < (Math.ceil(diameterOfDiamond / 2.0) ); i++)
                {
                    for(int j = (int) Math.ceil(diameterOfDiamond / 2.0); j > Math.ceil(diameterOfDiamond / 2.0) - i; j--)
                        System.out.print(" ");
                    for(int j = diameterOfDiamond - 2; j >= i * 2 - 1; j--)
                        System.out.print(diamondCharacter);
                    System.out.println();
                }
     
                System.out.println("Would you like to run this program again(yes/no): ");
     
                if(scan.next().equalsIgnoreCase("no"))
                    flag = false;
            }
     
        }
    }
    Last edited by thatguy; March 5th, 2012 at 10:03 PM.

Similar Threads

  1. Replies: 9
    Last Post: December 13th, 2011, 12:50 AM
  2. Replies: 3
    Last Post: November 18th, 2011, 08:33 AM
  3. Help really appreciated
    By Thermal_Vent in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2010, 08:42 AM
  4. any help is much appreciated
    By Schmitz14 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2009, 07:51 PM
  5. Need a little help. would be greatly appreciated
    By ryan29121 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 27th, 2009, 02:03 PM